您介意使用扫描码吗?
我知道这不是您正在寻找的简单、直接的解决方案,但恐怕没有。
所以我写这篇文章是希望,如果不是你,其他一些陷入困境的程序员也能找到有用的东西。
我也知道你是为 TASM 写的,但我忘记了,从 NASM 开始,但是转换应该很容易(只需添加段声明,从括号中取出段寄存器并添加 PTR) .
从硬件的角度来看,键盘似乎只重复最后按下的键1,所以如果两个玩家同时按下两个键,实际上只有一个是由键盘。
然而,软件在任何地方一次处理多个键,他们是如何做到的?
诀窍在于,当按键被按下时,键盘会发送两个代码(扫描代码):一个是在按键按下时(以及许多其他的,当它被关闭时)和一个当它发布时。
因此,该软件可以判断按键何时按下,而无需键盘发出进一步通知2。
我找不到任何处理按键向上和向下事件的interrupt service。
唯一的解决方案是直接处理扫描码。
虽然8042 chip 很简单,但不需要弄脏 IO 指令。
在不解释 8259A 和 IRQ 映射如何工作的情况下,只要说按下/释放键时使用AL 中的扫描码调用中断15h/AH=4fh 就足够了。
我们可以拦截该中断并检查扫描码是用于按下键还是按下键。
发布扫描码已设置 bit7。
我们可以有一个 128 字节的数组,每个字节代表任何可能的扫描码值 (bit0-6)3 并存储在每个元素 0ffh 如果扫描码指示按下或00h 如果它表示发布。
保留已处理扫描码的计数也很有用,因此程序可以通过简单的算术等待新的扫描码。
现在,我想你仍然很困惑。
我写了一个演示。
下面的程序,对于 NASM,等待您同时按 a 和 d 键退出。
警告 由于我们使用的是扫描码,这取决于键盘布局!
BITS 16
ORG 100h ;COM
;Setup ISR for the scancode
call init
;Clear screen
mov ax, 03h
int 10h
;Print command
mov ah, 09h
mov dx, strCommand
int 21h
_main:
;Wait for a change in the scancode tables
call wait_for_scancode
;Remove unused keystrokes
call remove_keystrokes
;Check if a is pressed
mov al, 1eh ;a
call is_scancode_pressed
jz _main
;Check if 'd' is pressed
mov al, 20h ;d
call is_scancode_pressed
jz _main
;Both are pressed, print bye and ...
mov ah, 09h
mov dx, strDone
int 21h
;... restore the ISR and ...
call dispose
;... exit
mov ax, 4c00h
int 21h
strCommand db "Press 'a' and 'd' to exit", 13, 10, 24h
strDone db "Bye",13,10,13,10,24h
;Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll L
; Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll
;Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll Ll L
;S C A N C O D E F U N C T I O N S
;Set the ISR
init:
push ax
mov ax, cs
mov WORD [old_isr_15 + 02h], ax
;old_isr_15 is now a far pointer to new_isr_15
call swap_isr_15 ;Swap the current isr15 with the one in old_isr_15
pop ax
ret
;Restore the original ISR
dispose:
call swap_isr_15 ;Swap the current isr15 with the one in old_isr_15
ret
;Swap the pointer in the IVT for int 15h with the pointer in old_isr_15
swap_isr_15:
push eax
push es
xor ax, ax
mov es, ax
cli
mov eax, DWORD [es: 15h*4]
xchg eax, DWORD [old_isr_15]
mov DWORD [es: 15h*4], eax
sti
pop es
pop eax
ret
;Wait for a change in the scancode table
wait_for_scancode:
cli ;Prevent the ISR from messing things up
;At least one scancode processed?
cmp WORD [new_scancode], 0
jne _wfs_found ;Yes
;No, restore interrupt so the CPU can process the prending ones
sti
jmp wait_for_scancode
;New scancode, decrement the count and restore interrupts
_wfs_found:
dec WORD [new_scancode]
sti
ret
;THe BIOS is still saving keystrokes, we need to remove them or they
;will fill the buffer up (should not be a big deal in theory).
remove_keystrokes:
push ax
;Check if there are keystrokes to read.
;Release scancodes don't generate keystrokes
_rk_try:
mov ah, 01h
int 16h
jz _rk_end ;No keystrokes present, done
;Some keystroke present, read it (won't block)
xor ah, ah
int 16h
jmp _rk_try
_rk_end:
pop ax
ret
;Tell if a scancode is pressed
;
;al = scancode
;ZF clear is pressed
is_scancode_pressed:
push bx
movzx bx, al
cmp BYTE [scancode_status + bx], 0
pop bx
ret
;AL = scancode
new_isr_15:
;Check for right function
cmp ah, 4fh
jne _ni15_legacy
;Save used regs
push bx
push ax
movzx bx, al ;BX = scancode
and bl, 7fh ;BX = scancode value
sar al, 07h ;AL = 0ffh if scancode has bit7 set (release), 00h otherwise
not al ;AL = 00h if scancode has bit7 set (release), 0ffh otherwise
;Save the scancode status
mov BYTE [cs:bx + scancode_status], al
;Increment the count
inc WORD [cs:new_scancode]
pop ax
pop bx
_ni15_legacy:
;This is a far jump, in NASM is simply jmp FAR [cs:old_isr_15]
;Ended up this way for debug
push WORD [cs: old_isr_15 + 02h]
push WORD [cs: old_isr_15]
retf
;Original ISR
old_isr_15 dw new_isr_15, 0
;Scan code status table
scancode_status TIMES 128 db 0
;Scan code count
new_scancode dw 0
您只需要使用:
-
init 设置扫描码监听。
-
dispose 拆除扫描码窥探。
-
is_scancode_pressed 知道某个键是否被按住。
其他所有东西,包括wait_for_scancode 和remove_keystrokes 都是附属的,只是为了让你的程序表现得非常好。
如果您想查找与某个键关联的扫描码,您可以使用this other program,它会显示一个带有按下的扫描码的表格(按 ESC 退出)。
例如,如果我按 a 和 d 我会得到演示中使用的值
它只是上面演示的一个变体。
1我创建了this 引导程序(用于 NASM)来测试我的假设,至少在我的硬件中。
2 实际上,重复功能仅在打字软件中有用,其他所有应用程序,如游戏,明确检查按键状态都不需要它。
3 实际上有扩展(多字节扫描码),但这里忽略它们。坚持使用普通键!