delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/01/09/10:06:42

From: bk039 AT FreeNet DOT Carleton DOT CA (Raymond Martineau)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Problem with kbhit()
Date: 9 Jan 2001 14:30:22 GMT
Organization: The National Capital FreeNet
Lines: 43
Message-ID: <93f79u$1p4$1@freenet9.carleton.ca>
References: <3A5AE4E7 DOT 85120936 AT student DOT uni-ulm DOT de>
NNTP-Posting-Host: freenet10
X-Trace: freenet9.carleton.ca 979050622 1828 134.117.136.30 (9 Jan 2001 14:30:22 GMT)
X-Complaints-To: complaints AT ncf DOT ca
NNTP-Posting-Date: 9 Jan 2001 14:30:22 GMT
X-Given-Sender: bk039 AT freenet10 DOT carleton DOT ca (Raymond Martineau)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Karsten Schmidt (Karsten DOT Schmidt AT student DOT uni-ulm DOT de) writes:
> Hi!
> 
> I'm trying to detect when the user hits the keyboard. 
> Unfortunately the kbhit() function is always true 
> after the first key has been hit. Therefore I'm not able to
> detect any further hits.
> 

If you want to detect any further keypresses, you will need to clear the
keyboard buffer.  A command such as getchar() or getch() will do so.

> 
> I've tracked down my problem to the following program:
> 
> #include <stdio.h>
> #include <stdlib.h>
> #include <pc.h>                     
> #include <dos.h>
> 
> int main (void)
> {  while(!kbhit())    // Wait until key pressed
>    {  printf(".");
>       delay(1000); }
>    printf("\n");

This section is okay, but you might want to read the character from the
keyboard buffer.

>    while (kbhit())    // Wait until key released
>    {  printf("+");
>       delay(1000);}

Remove this section.  kbhit() returns true if there are characters in the
keyboard buffer, and false when the buffer is empty.  A double keypress,
or a keypress betheen the getch() and the loop will cause an infinite loop.

If you want to detect if a key has been released, you will need to create
an interrupt handler to manage the keyboard, or use the Allegro Low
Level Game Routines (commonly known as Allegro).

--
Raymond Martineau - bk039 AT freenet DOT carleton DOT ca

- Raw text -


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