Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Key trapping

Status
Not open for further replies.

adholioshake

Programmer
Feb 9, 2003
136
0
0
I wrote a TSR program for storing keyscan codes in a buffer, but it didn't quite work. Here is relevent code:

;( Start of code extract )
;.........
keycount dw 1 ; Counter
keyscan db 100 dup (?) ; Buffer

; Key int replaces interrupt 9 (key press)
keyint:
cli
push ax ; save registers (!)
push bx
push cx
push dx
push sp
push si

cmp keycount,100 ; Buffer full
je keyintret
in al,60h
mov bx,keycount
mov keyscan[bx],al
inc keycount

keyintret:
pushf
call cs:eek:ldhandler ; call old int 09h
pop si
pop sp
pop dx
pop cx
pop bx
pop ax
iret
;..........
;( End of code extract )

The interrupt is replaced and works OK, but pressing a key generates two key scan codes in the buffer. The first is the actual keyscan code pressed, the second is the keyscan pressed + 128. Why does this happen ?
 
the routine you are calling ends with an iret.
i would pop your stack then perform a far JMP to the old interrupt vector.
tell me how you get on :)

"There are 10 types of people in this world, those who know binary and those who don't!"
 
addition:

you only need to push ax,bx and flags as these are the only registers that you are likely to change.

also beware of DS - if any program changes this then you will be in trouble.

maybe try this:

pushf
push ax
push bx
push ds
push cs ;make data segment same as code segment
pop ds ;real mode assumed (execption when PE set)
<stuff>
pop ds
pop bx
pop ax
popf ;finish with original flags
jmp far ptr cs:eek:ldhandler

it could be something to do with the EOI command required before control is returned to the interrupted program.
&quot;There are 10 types of people in this world, those who know binary and those who don't!&quot;
 
Receiving 2 scancodes means that your code works!

Since reading port 60h is really low-level, as much information as possible is returned: namely the scancode for a push and the scancode for a release. This way, you can make applications (read: games) in which you can press more than one button at the same time to do something.

Note that the keyboard itself is not always capable of certain key combinations. Regards,
Bert Vingerhoets
vingerhoetsbert@hotmail.com
Don't worry what people think about you. They're too busy wondering what you think about them.
 
Bertv100,
thanks for answer. May I add a question?
I've never done a key-interrupt replace. Presumeably adholioshake is getting 2 additions to his buffer because his interrupt is called twice (his routine appears to make a single addition to the key-buffer per call)? So am I right in thinking that a single key-press causes 2 interrupt 9's??
Li
 
Pressing a key causes an interrupt call, releasing it does so too. Regards,
Bert Vingerhoets
vingerhoetsbert@hotmail.com
Don't worry what people think about you. They're too busy wondering what you think about them.
 
Thanks for the replies.

I did think it could be two interrupts but wasn't sure.
The second scan code does indicate the key release by having the 128-bit of the byte set to 1. I think another bit might be used for other status of the key. e.g. when you press the insert key. Somebody might want to investigate this further.
Having sorted out this TSR, I am know working on a windows based Keycapture - apparently MS replaces most DOS interrupts in windows(!)
Adonai :)
 
cool! &quot;There are 10 types of people in this world, those who know binary and those who don't!&quot;
 
the status of the &quot;special&quot; keys are stored in a byte in the BDA (bios data area) starts at 0400h (cant remember the byte) - i can look it up if you want.

anyway, im under the impression that this is set by the bios interrupt (which you are replacing) hence the interrupt should fire when pressed/depressed (but im not sure so maybe some1 who know could shed some light)

&quot;There are 10 types of people in this world, those who know binary and those who don't!&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top