[comp.lang.c] keyboard buffer routine

meo3@jaguar.ucs.uofs.edu (OGRINZ MICHAEL E) (03/09/91)

	A quick question about a problem I have thus far been unable to
implement "bug-free". What's the best way to flush the keyboard buffer
on the PC ? (I'm using TC 2.0)

Thanks,

Mike

syrjanen@serifos.Helsinki.FI (Seppo Syrjanen) (03/12/91)

In <325@platypus.uofs.edu> meo3@jaguar.ucs.uofs.edu (OGRINZ MICHAEL E) writes:
>What's the best way to flush the keyboard buffer on the PC ? (I'm using TC 2.0)

	while (kbhit()) getch();

Seppo

markh@csd4.csd.uwm.edu (Mark William Hopkins) (03/17/91)

In article <325@platypus.uofs.edu> meo3@jaguar.ucs.uofs.edu writes:
>
>	A quick question about a problem I have thus far been unable to
>implement "bug-free". What's the best way to flush the keyboard buffer
>on the PC ? (I'm using TC 2.0)

Set the head pointer of the keyboard buffer to the tail pointer.

------------------------------------------------------------
This is a version written in Quick C.  I don't think you'll have much
trouble porting it to Turbo C.
--------------------
#include <dos.h>

static unsigned /* Keyboard buffer pointers */
   far *HeadP = (unsigned far *)0x0040001a,
   far *TailP = (unsigned far *)0x0040001c;

void FlushKbd(void) [ *HeadP = *TailP; }

------------------------------------------------------------
This does by-pass BIOS.  If you don't want that, then here's another version
written using BIOS routines (supplied in the Quick C library):
--------------------
#include <bios.h>
void FlushKbd(void) {
   while (_bios_keybrd(_KEYBRD_READY)) 
      (void)_bios_keybrd(_KEYBRD_READ);
}

------------------------------------------------------------
And a version hand-compiled in assembly, if Turbo C doesn't have BIOS keyboard
handlers in its library:
--------------------
_KbdInt EQU 16h   ;;; BIOS keyboard services
_KBD_READ  EQU 0
_KBD_READY EQU 1

_FlushKbd PROC NEAR
   FlushLoop:             ;;; while (1) {
      mov AH, _KBD_READY
      int _KbdInt         ;;;    z = _bios_keybrd(_KEYBRD_READY);
   jz FlushBreak          ;;;    if (z) break;
      mov AH, _KBD_READ
      int _KbdInt         ;;;    (void)_bios_keybrd(_KEYBRD_READ);
   jmp FlushLoop          ;;; }
   FlushBreak:
_FlushKbd ENDP