delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/10/08/03:17:05

Newsgroups: comp.os.msdos.djgpp
Subject: Re: How to empty the keyboard buffer
From: richard AT stardate DOT bc DOT ca (Richard Sanders)
X-Newsreader: WinVN 0.99.9 (Released Version) (x86 32bit)
References: <Pine DOT SUN DOT 3 DOT 91 DOT 991005095415 DOT 2899L-100000 AT is> <7tibo0$44f$1 AT rohrpostix DOT uta4you DOT at>
MIME-Version: 1.0
NNTP-Posting-Host: wlp36.rapidnet.net
Message-ID: <37fd6535_2@news.vphos.net>
Date: 7 Oct 1999 20:29:57 -0800
X-Trace: 7 Oct 1999 20:29:57 -0800, wlp36.rapidnet.net
Lines: 43
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

>
>Okay, the shortest test program is the following:
>
>#include <stdlib.h>
>#include <stdio.h>
>
>#include "allegro.h"
>
>int main()
>{
>  int c;
>
>  allegro_init();
>  install_timer();
>  do
>  {
>    c = getch();
>    rest(1000);
>    while (kbhit())
>      c = getch();
>  }
>  while (c != 27);
>}

I have always found it better to empty the keyboard buffer before attempting to 
get keystroke. This ensures that any key strokes captured were made when your 
program was looking for one, not some leftover stuff or someone just playing 
on the keyboard.  Something like this.

/* start empty the keyboard buffer */
while( kbhit())
  getch();
/* end empty the keyboard buffer */

/* start key capture */
do
{
  while(!kbhit()); /* loop until a key is pressed */
  c=getch();
}
while( c!=27 );  /* loop until the desired key is pressed */
/* end key capture */

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019