Console and keys

  • Thread starter Thread starter rozrabiak
  • Start date Start date
R

rozrabiak

Hello!
How can I get key code in console program? Eg. When I press ENTER then
he show me code 13, when CTRL then code of CTRL key, ALT...

In C# Helpis only one example, but they not detect CTRL, ALT, ESC key press.
 
Gregory,

This is a little tricky. First, you have to create your own input
buffer for the console through a call to CreateConsoleScreenBuffer. Once
you have that, you have to set it to the active screen buffer by making a
call to SetConsoleActiveScreenBuffer. After that, you have to call
SetConsoleMode with a parameter of ENABLE_ECHO_INPUT, so that the characters
are written to the buffer one by one, and not when a line is input.
Finally, to get the input, you would have to call ReadConsole.

All of these are API functions which you would have to declare and call
through the P/Invoke layer.

Hope this helps.
 
Back
Top