Question : C++ console programming: How can I listen for input for a limited amount of time?
The basics: I’m writing a console program that needs to get direct input from the user (one of the arrow keys), but go ahead and do the default option after a certain amount of time (1 second in my case because it’s a game).
Currently, I’ve gotten it to listen for direct input and react, but I can’t seem to find a way to integrate the time-out feature. Below is my current code:

//Keypress tester
//12/5/2009
//Mike W

#include
#include
#include
#include

using namespace std;

char Momentum (char);
VOID ErrorExit(LPSTR);
VOID KeyEventProc(KEY_EVENT_RECORD);

int main(VOID)
{
HANDLE hStdin;
DWORD cNumRead, fdwMode,fdwSaveOldMode,i;
INPUT_RECORD irInBuf[128];
int counter=0;

//get the standard input handle

hStdin = GetStdHandle(STD_INPUT_HANDLE);
if (hStdin == INVALID_HANDLE_VALUE)
ErrorExit(“GetStdHandle”);

//Save the current input mode, to be restored on exit.

if (! GetConsoleMode(hStdin, &fdwSaveOldMode) )
ErrorExit(“GetConsoleMode”);

//Enable the window and mouse input events.

fdwMode = ENABLE_WINDOW_INPUT;
if (! SetConsoleMode(hStdin,fdwMode) )
ErrorExit(“SetConsoleMode”);

//Loop to read and handle the input events.

while (1)
{
if (! ReadConsoleInput(
hStdin, //input buffer handle
irInBuf,//buffer to read into
128,//size of read buffer
&cNumRead) ) //number of records read
ErrorExit(“ReadConsoleInput”);

//Dispatch the events to the appropriate handler.
for (i = 0; i < cNumRead; i++)
{
switch(irInBuf[i].EventType)
{
case KEY_EVENT: //keyboard input
KeyEventProc(irInBuf[i].Event.KeyEvent);
break;

default:
break;
}
}
}
return 0;
}

VOID ErrorExit (LPSTR lpszMessage)
{
fprintf(stderr, "%sn", lpszMessage);
ExitProcess(0);
}

VOID KeyEventProc(KEY_EVENT_RECORD ker)
{
if(ker.bKeyDown) {
printf("Key Pressed:");
int x, y;
x = ker.wVirtualScanCode*100;
y = ker.wVirtualKeyCode;
switch (x+y)
{case 7537: //Left arrow key
cout << Momentum('l');
break;

case 7739://Right arrow key
cout << Momentum('r');
break;

case 7238://up arrow key
cout << Momentum('u');
break;

case 8040://down arrow key
cout << Momentum('d');
break;

default:
cout << Momentum('n');
break;
}

}
}
//this returns a character signifying the current direction the player is traveling. it is set to whatever direction the user enters if it is one of the arrow keys.
char Momentum (char input)
{
static int momentum;
if (input != 'n')
momentum = input;
return momentum;
}

//END CODE

More info: This is going to be used in a snake-like game for the console. Sorry if my coding is bad, I'm in a first semester class so I hope to get better... Anyway any help on how to integrate them would be appreciated. Thanks!
dispatch consoles

Best answer:

Answer by Ratchetr
You can use WaitForSingleObject on the stdin handle with a timeout.

Try adding this at the start of your while loop, before the call to ReadConsoleInput:

DWORD res;
res = WaitForSingleObject(hStdin,1000);
if(res == WAIT_TIMEOUT)
{
   // Timeout, no input. Do something then try again..
   cout << '.';
   continue;
}