>I know that getchar(), cin, getch() commands don't allow to do that.
getch (and getche) does, but it isn't a standard function and won't exist on all implementations. What compiler and operating system do you use?
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
The advice by Warburg is a valuable hint, but the WIN32 API funcion ReadConsoleInput waits for a keypress or a mouseclick. It must be supplemented with a wait function. The following function returns zero when nothing was pressed and ASCII code when something "ASCII" was pressed. I hope that this solution really works (even in "console" applications), but nothing is 100% ...
int keytest( void )
{
CHAR ch;
DWORD dw;
HANDLE keyboard;
INPUT_RECORD input;
keyboard = GetStdHandle(STD_INPUT_HANDLE);
dw = WaitForSingleObject( keyboard, 0 );
if( dw != WAIT_OBJECT_0 )
return 0;
dw = 0;
// Read an input record.
ReadConsoleInput(keyboard, &input, 1, &dw);
ch = 0;
// Process a key down input event.
if( !( input.EventType == KEY_EVENT
&& input.Event.KeyEvent.bKeyDown ) )
{
return 0;
}
// Retrieve the character that was pressed.
ch = input.Event.KeyEvent.uChar.AsciiChar;
// Function keys filtration
if( input.Event.KeyEvent.dwControlKeyState &
( LEFT_ALT_PRESSED | LEFT_CTRL_PRESSED | RIGHT_ALT_PRESSED |
RIGHT_CTRL_PRESSED )
)
return 0;
// if( ch == 13 )
// ...; // enter pressed
return ch;
}
you do realize your replying to a post from 2005
zeroliken
Veteran Poster
1,106 posts since Nov 2011
Reputation Points: 201
Solved Threads: 162