Hi there.
I want to ask, how to get a keyboard input in DOS.
I have tried many of interrupts, with various AH values, but nothing works.
Either it waits for a keystroke, or if a keystroke is present, it is not removed from the keyboard buffer.
I want to develop a simple game. Is there any way to get a keystroke directly?
Thanks for all ideas.
Comments
Interrupt 16h AH = 0 is the keyboard interrupt that will get deal with a keypress. When called, it waits for a keypress and returns the scancode of the key that was pressed in AH and the ASCII code of it in AL.
You say you've tried some interrupts.. have you tried this one?
Sorry, just re-read your original post. If you don't want the computer to wait for a keypress, you could try int 16 AH = 1. I haven't tried it myself, but the descriptions I've found sound right for what you want.
Thanks
Phillid
I have tried this INT.
The problem is, if a keystroke is present, it is not removed from the keyboard buffer. Like holding (or pressing) this key forever.
Can I clear somehow the keyboard buffer?
Thanks for help.
[code]
;------------------------------------------------------------------------------
;FLUSH KEYBOARD BUFFER
;Inputs:
;Outputs:
;Changes: Flushes the Keyboard Buffer
;------------------------------------------------------------------------------
FlushKbdBuff:
PUSH AX ;Save used registers
F10: ;Loop to here for each key
CALL GetKey ;Get a key from the keyboard buffer
JNZ F10 ;If there was one, get another
POP AX ;Restore used registers
RET
;------------------------------------------------------------------------------
;READ CHARACTER FROM THE KEYBOARD BUFFER
;Inputs:
;Outputs: AH = Keyboard scan code
; AL = ASCII value of keypress (0 if extended ASCII)
; AX = 0 if no key is waiting
;Changes: ZF = Set if no key in buffer (AX = 0)
; = Clear if a key was found (AX = key)
;------------------------------------------------------------------------------
GetKey:
MOV AH,1 ;Service 1 (Keystroke waiting?)
INT 16h ;Do It
JZ >K10 ;If no key waiting, we're done
XOR AH,AH ;If a key is waiting, service 0 (Get keystroke)
INT 16h ;Do It
JMP >K90 ;We're done
K10: ;No keystroke waiting
XOR AX,AX ;Make sure AX=0
K90: ;We're done
OR AX,AX ;Set the found/not found flag
RET
[/code]
[b]Bret[/b]
Thank you wery much!
It works.
I did not know, that I have to use the int 16h two times.
With AH 1 and next with AH 0.
Many thanks again.
Best regards.