Mail Archives: djgpp/2001/01/09/10:06:42
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 -