How do i check to see if a key is being pressed while in a "while()" loop?
Printable View
How do i check to see if a key is being pressed while in a "while()" loop?
If you're in a Windows app - your window will receive a WM_CHAR message.
For console apps, try the getch() function. Unfortunately I think that doesn't return until a key is pressed...
Use the _getch function.
Code:#include <conio.h>
#include <ctype.h>
void main()
{
char ch;
_cputs( "Enter a letter: " );
// Loop until "Y" is pressed
do
{
ch = _getch();
ch = toupper( ch );
}
while( ch != 'Y');
}