Keyboard input in DOS

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.


  • Hi and welcome.

    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.

  • [b]Phillid[/b]

    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.
  • INT 16.01 should retrieve the keystroke from the keyboard buffer and also clear it from the buffer. You may be doing something wrong in your testing. Here's some code I have in several of my programs (the > is use in my assembler (A86) to indicate a forward label reference):

    ;Changes: Flushes the Keyboard Buffer
    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

    ;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)
    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

  • [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.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!