www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/01/17/11:38:44

Date: Sun, 17 Jan 1999 18:37:23 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: Simon <simon AT magicsoftware DOT freeserve DOT co DOT uk>
cc: djgpp AT delorie DOT com
Subject: Re: Flusing keyboard buffer
In-Reply-To: <36A20315.588B@magicsoftware.freeserve.co.uk>
Message-ID: <Pine.SUN.3.91.990117183027.24432C-100000@is>
MIME-Version: 1.0
Reply-To: djgpp AT delorie DOT com

On Sun, 17 Jan 1999, Simon wrote:

> >        while (kbhit ())
> >          getkey ();
> 
> I meant to say that if the buffer contains keystrokes that were from
> some time ago and I want to totally disregard them and wait for new
> presses. I want do some code such as:
> 
> //flush old unwanted keys
>     flush_kb_buffer();
> //wait for new presses
>     while (kbhit ())
>           getkey ();

I'm not sure I understand what you are after.  I think that the while 
loop I suggested does exactly what you wanted, i.e. it removes any 
pending keystrokes from the keyboard buffer.  When the buffer is empty, 
the loop will be terminated.  You can then start a new loop that waits 
for the user to press a key, like this:

	while (kbhit ())
	  getkey ();

	while (!kbhit ())
	  __dpmi_yield ();  /* idle waiting for the user to press a key */

	/* They pressed a key; get it.  */
	key = getkey ();

Of course, if the user presses a key while you are in the first loop, that
key will be lost.  AFAIK, there's nothing you can do about that, though
(unless you hook the keyboard and process the interrupts yourself), since
the keypresses don't come with any time information which you could use to
know whether this is an ``old'' or a ``new'' keypress. 

- Raw text -


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