有没有一种方法可以在基于DOS的程序中读取键盘修改键,如ALT或CTRL



我知道您可能会轮询键盘缓冲区以获取修改键,如ALT或CTRL。但即使在旧的DOS程序中,当我按下这些键时也有一个动作(例如,通过按下ALT来更改菜单按钮的颜色(。在DOS中有办法获得这些密钥吗?这是怎么做到的?我认为在BASIC中没有解决方案,尽管BASIC有一些ON事件处理程序可用。欢迎对这些问题提出任何建议或建议。

您可以查看BIOS数据区中线性地址1047处的KeyboardStatusFlags。对于Alt键,您检查第3位,对于Ctrl

DEF SEG = 0
DO
IF PEEK(1047) AND 8 THEN
PRINT "ALT is pressed"
EXIT DO
ELSEIF PEEK(1047) AND 4 THEN
PRINT "CTRL is pressed"
EXIT DO
END IF
LOOP

回答评论

是否还有一种方法可以通过窥探地址来获得按下的键(ASCII值(?

同样,您可以在键盘缓冲区(循环缓冲区(中找到此信息。BIOS维护一个字大小的指针,指向存储下一个可用密钥的位置(HEAD(,以及一个字尺寸的指针,指针指向存储最近缓冲的密钥的后面的位置(TAIL(。如果HEAD等于TAIL,则键盘缓冲区为空。在这种情况下,INKEY$将返回一个空字符串。

Head% = PEEK(1050) + 256 * PEEK(1051)
Tail% = PEEK(1052) + 256 * PEEK(1053)
IF Head% <> Tail% THEN
Ascii% = PEEK(1024 + Head%)
Scan% = PEEK(1024 + Head% + 1)
ELSE
Ascii% = 0
Scan% = 0
END IF

上述代码的"优势"在于,您可以预览键盘缓冲区中下一个可用的键(如果有的话(。钥匙未取出。INKEY$可以提供相同的信息,但也会删除密钥

另一个解决方案是Michael Petch的答案:堆栈溢出获取ASCII值

; Assemble with nasm -f bin getkeyh.asm -o getkeyh.com
GetKeyH:
push bp
mov  bp, sp
les  bx, [bp+6]            ; ES:BX = address of variable to return value in
; [bp+0] is where BP was pushed
; [bp+2] is where the 32-bit far return address is
; [bp+6] is where last parameter is
; Parameters are pushed on stack left to right
; like pascal calling convention.
in   al,60h                ; Get scancode from keyboard
xchg dx,ax
xor  ax,ax                 ; assume no key (AX=0)
test dl,10000000b          ; is it key up event?
jnz  short getkeyhD        ;     if it is return 0 (in AX)
mov  al, dl                ; Otherwise keydown, AX = scan code
getkeyhD:
mov  [es:bx], ax           ; Update variable with scancode so BASIC can read it.
pop bp

可与MASM/JWASM/Turbo汇编程序一起使用的版本:

; Assemble and link with Turbo Assembler to getkeyh.com file with: 
; tasm getkeyh.asm
; tlink /t getkeyh
;
; You can use JWASM a MASM clone available on MacOS/Linux/Windows to
; build getkeyh.com . You can use:
; jwasm -bin -Fo=getkeyh.com -0 getkeyh.asm
;
; -0 generates code that can run on 8088/8086 processors
; -1 for 186+ processors
; -2 for 286+ processors
;
; MASM 6.0+ and Segmented Linker LINK.EXE (5.60) can generate getkeyh.com:
; masm getkeyh.asm;
; link /t getkeyh,getkeyh.com;
;
; MASM5.x doesn't support ".model tiny" you have to use ".model small"
; and use LINK.EXE 5.60:
; masm getkeyh.asm;
; link /t getkeyh,getkeyh.com;

.model tiny                    ; We will generate a COM file

.code
org 100h                       ; COM Programs have an ORG 100h

GetKeyH PROC
push bp
mov  bp, sp
les  bx, [bp+6]            ; ES:BX = address of variable to return value in
; [bp+0] is where BP was pushed
; [bp+2] is where the 32-bit far return address is
; [bp+6] is where last parameter is
; Parameters are pushed on stack left to right
; like pascal calling convention.

in   al,60h                ; Get scancode from keyboard
xchg dx,ax
xor  ax,ax                 ; assume no key (AX=0)
test dl,10000000b          ; is it key up event?
jnz  short getkeyhD        ;     if it is return 0 (in AX)
mov  al, dl                ; Otherwise keydown, AX = scan code
getkeyhD:
mov  es:[bx], ax           ; Update var with scancode so Turbo Basic can read it
pop bp                     ; Do not use `RET`, Turbo Basic will return for us
GetKeyH ENDP

END GetKeyH                    ; Entrypoint is GetKeyH

与Turbo基本程序部分:

SUB GetKeyH INLINE
$INLINE "getkeyh.com"
END SUB
CLS
DO
LOCATE 10, 10
Call GetKeyH (scancode%)
PRINT "Key = "; HEX$(scancode%); "    "
LOOP UNTIL INKEY$ = CHR$(27)
END

最新更新