The idea of the following sample is to use a wait function to see if there is console input. If there is input then the code looks for a key up record and returns the character value. Input records in the buffer that are not KEY_EVENT records are ignored. If the buffer is empty (or only contains events that are not KEY_EVENT records) the code returns EOF.
int GetChNoWait()
{
INPUT_RECORD rec;
HANDLE hInput = NULL;
hInput = GetStdHandle(STD_INPUT_HANDLE);
while (WaitForSingleObject(hInput, 0) == WAIT_OBJECT_0)
{
DWORD dwRead;
ReadConsoleInput(hInput, &rec, 1, &dwRead);
if (rec.EventType != KEY_EVENT)
continue;
else if(rec.Event.KeyEvent.bKeyDown)
continue;
else
{
#ifdef UNICODE
return (int) rec.Event.KeyEvent.uChar.UnicodeChar;
#else
return (int) rec.Event.KeyEvent.uChar.AsciiChar;
#endif
}
}
return EOF;
}