PDA

View Full Version : press any key



taveren
02-22-2002, 10:39 AM
does anyone know how to implement "to continue press any key" in C on dos
I just need to know how to know when any key is being pressed on the keyboard.
Also i would like to know how to tell which key is being pressed if anyone knows.
Thanks

Goof Program
02-22-2002, 01:44 PM
Try the getch(); for just one key press or getchar(); for more then 1 key press.

if you dont want the program to stop and wait for the key press use the kbhit(); and then later use getch(); or getchar(); to read what key(s) they pressed.

taveren
02-22-2002, 02:06 PM
thanks for the reply but with getch() and getchar() the user has to press return before the program moves on. i want the program to move on immediately after any key is pressed on the keyboard
any hints

Barjor
02-22-2002, 02:48 PM
You could make a loop that loops until the user presses a key using the kbhit() for breaking




#include <conio.h>

while(kbhit() == 0)
{
DoCoolThings();
}

taveren
02-22-2002, 03:42 PM
that worked well but now the character of the key the user presses shows up at the next command prompt. how do i get rid of this character so that it doesnt show up anywhere

ihsir
02-23-2002, 04:50 AM
you could use clrscr(); after using the kbhit() loop

Pappy1942
02-25-2002, 07:48 AM
Hi,
Try this.

#include <conio.h>
#include <stdio.h>
int main()
{
char a;
while(!kbhit())
{
//do whatever
}
getch();// captures the char from kbhit

return 0;
}

Sekti
02-26-2002, 06:20 PM
use
#include <iostream.h>
#include <stdlib.h> /* needed for system() */

int main()
{
system("pause");
return 0;
}

Output:

Preass any key to continue...

Bam! :D