is there a way to read the Keyboard Modifier Key such as ALT or CTRL in DOS-Based Progarmms?

Viewed 155

I do know that you might polling the keyboard buffer to get the Modifier Keys such as ALT or CTRL. But even in old DOS Programms there was an Action when I just pressed these keys (f.e. to change the Color of the MENU buttons by pressing ALT). Is there a way in DOS to get these Keys? How is this be done? I think in BASIC there would be no solution although BASIC has some ON Eventhandler available. Any recommands or advices to this questions are welcome.

2 Answers

You can look at the KeyboardStatusFlags at linear address 1047 in the BIOS data area. For the Alt key you examine bit 3, and for the Ctrl key you examine bit 2. Next QBASIC program does exactly that:

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

Answering a comment

Is there also a way to get the KEY Pressed (ASCII VALUE) by peeking an address?

Again you can find this info in the keyboard buffer (a circular buffer). BIOS maintains a word-sized pointer to the place where the next available key is stored (HEAD), and a word-sized pointer to the place behind where the most recently buffered key is stored (TAIL). If HEAD equals TAIL, then the keyboard buffer is empty. INKEY$ would return an empty string in this case.

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

The 'advantage' of above code is that you can preview what key (if any) is available next in the keyboard buffer. The key is not removed. INKEY$ can give the same info but will remove the key also.

Another solution is the Answer of Michael Petch: Stackoverflow-get ASCII VALUE

; 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
    

A version that can be used with MASM/JWASM/Turbo Assembler:

; 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

with the Turbo Basic Program part:

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
Related