Howdy, Stranger!

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


Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Keyboard input in DOS

AllerAller Posts: 3Member
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.


  • phillidphillid Posts: 3Member
    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.

  • AllerAller Posts: 3Member

    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.
  • BretBret Posts: 114Member
    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
  • AllerAller Posts: 3Member


    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.