x86 Assembly

Moderators: None (Apply to moderate this forum)
Number of threads: 4563
Number of posts: 16029

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Keyboard input in DOS Posted by Aller on 19 May 2012 at 1:26 AM
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.
Report
Re: Keyboard input in DOS Posted by phillid on 19 May 2012 at 1:45 AM
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.

Thanks
Phillid
Report
Re: Keyboard input in DOS Posted by Aller on 19 May 2012 at 3:02 AM
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.
Report
Re: Keyboard input in DOS Posted by Bret on 19 May 2012 at 8:42 AM
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):

;------------------------------------------------------------------------------
;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
Report
Re: Keyboard input in DOS Posted by Aller on 21 May 2012 at 5:17 AM

Bret

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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.