Search code examples
c++keyboardconsolekeypress

Console Keyboard hit detection and interpretting


Hey so it's been reccomended to me that i should use the kdhit() function to detect a keypress in a console window:

"If it's Windows, the kbhit( ) function is the guy you want. If it's *nix, here's a kbhit( ) emulator. kbhit( ) returns immediately (no blocking) with a flag that says that there is or is not a keyboard character waiting to be read. You can test the flag to see if you should issue a keyboard read."

However i would like to know the easiest, hopefully standard way of reading the key buffer or what he calls the flag. Any tips? Thanks!


Solution

  • Use _kbhit() to test if a key is pressed. and when it is use _getch() to get the value. (note _getch() can return 0 or 0xE0 for special keys and call _getch() again for that value) for displaying the characters while reading use _getche().