[comp.os.msdos.programmer] wait for a keystroke

fordke@infonode.ingr.com (Keith Ford x8614) (12/07/90)

I am writing a program for my daughter where I have an animated
character that moves whenever "any" key is pressed.  The key
press simply causes to chatacter to continue along it's programmed
path.  If she decides to hold a key down forever, I would like for
that to be ignored and to not fill up the buffer and start beeping,
but just let me know when a key has been pressed when I ask.
I.E. a key press is what causes the action to continue.  Any help
would be appreciated.
-- 
| ...!uunet!ingr!fordke    OR    fordke@ingr.com
| Micro Magic BBS     (Fidonet: 1:373/12,   MaBell: +1 205 830 2362)
| "and the Trees are all kept equal by hatchet, axe, and saw." -Rush

jdb@reef.cis.ufl.edu (Brian K. W. Hook) (12/15/90)

I had the same problem with a flight simulatr that I had been writing.
If you are using Turbo C, the following code should do the trick:

if (kbhit())
  ch=getch()
else {
  dostuff()
}

with QuickC I believe that you use kbdhit().  these check the keystroke
buffer for a key hit.  Also, check out _bios_keybrd() (QC) and bioskey()
under TC.

If you want to just get the last key pressed and flush the buffer of other
keystrokes, the following will work under turbo C.

#include <bios.h>

int getLastKeyPressed ( void )
{
int chTemp;

   while (bioskey(1)!=0) {
      chTemp=getch();
      if (bioskey(1)==0)
         return chTemp;
   }
}

Note that getch() will return a 0 if an extended key was pressed (ie.
F1-F12, arrow keys, page up, etc.) and that you must then do an other
getch() to get the scan code (most PC programming books have this in 
them).