In C++, when you develop console applications, sometimes you may need to perform an action in response to key events or key presses such as gameplay actions in games. C++ supports many game engines, and some professional C++ IDEs provide features suitable for a C++ Game Engine so it can support event handling along with 2D and 3D applications via their UI libraries. Actually, there are many ways to detect key inputs. In this article, we explain one of the ways that you can detect and use key presses in C++ on Windows.
Table of Contents
How to detect and use key presses in C++ on Windows?
There are many ways to detect key presses in C++. Key input detection may depend on device architecture – such as having a physical or an on-screen keyboard, the available micro-chip architecture, the operating system, and even things like specialized input devices like streamer consoles. Here, we will explain how you can detect various types of keypresses on Windows console applications in C++, C++ Builder, Dev-C++ and in other C++ compilers running on Windows.
To represent a single key, you may use either a code or a string. There are tables that show a complete list of codes and strings that you may use to represent each key. These tables do not show a string representation for some keys because the UI framework is not responsible for the string representation of those keys. Instead, the operating system where your application is running provides the string representation of those keys. These tables do not show those string representations provided by the operating system because they may vary; for example, Windows provides locale-specific key names.
Microsoft listed windows keysand their decimal values here: https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
Also, windows keys and shortcuts are officially listed in Embarcadero’s DocWiki here : https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Representing_Keys_and_Shortcuts
How to detect and use key presses in C++ on Windows using ReadConsoleInput?
If you want to get key presses in console applications on Windows, you can use ReadConsoleInput
method.
Here is the Syntax for ReadConsoleInput() method,
1 2 3 4 5 6 7 8 |
BOOL WINAPI ReadConsoleInput( _In_ HANDLE Console_Input_Handle, _Out_ PINPUT_RECORD lnput_Buffer, _In_ DWORD Input_Length, _Out_ LPDWORD Number_Of_Events_Read ); |
for example we can use this as we show below.
1 2 3 |
ReadConsoleInput( hIn, &inp, 1, &num_of_events); |
Where these parameters are defined before as given here;
1 2 3 4 5 6 |
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE); HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); INPUT_RECORD inp; DWORD num_of_events; |
Is there a simple example of how to detect and use key presses in C++ on Windows?
In this example, we use ReadConsoleInput()
method to read inp
input. Then we can get Virtual Key Code with inp.Event.KeyEvent.wVirtualKeyCode
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE); HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); INPUT_RECORD inp; DWORD num_of_events; ReadConsoleInput( hIn, &inp, 1, &num_of_events); switch (inp.EventType) { case KEY_EVENT: switch(inp.Event.KeyEvent.wVirtualKeyCode) { } } |
As in this example, if you want to detect characters like ‘a’, ‘s’, ‘d’, you can use inp.Event.KeyEvent.uChar.AsciiChar
like so:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE); HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); INPUT_RECORD inp; DWORD num_of_events; ReadConsoleInput( hIn, &inp, 1, &num_of_events); switch (inp.EventType) { case KEY_EVENT: switch(inp.Event.KeyEvent.uChar.AsciiChar) { } } |
However, both of these codes may not work, because you will not reach be able to detect your key press due to timing. You should instead do this check frequently, for example in a loop as given example below.
Is there a full example to detect and use key presses in C++ on Windows?
Here is a full C++ example that detects WASD keys, space key, F1 key and Esc Key;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
#include <iostream> #include <windows.h> int main() { HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE); HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); INPUT_RECORD inp; DWORD num_of_events; bool exit = false; do { // Read Console Input ReadConsoleInput( hIn, &inp, 1, &num_of_events); switch (inp.EventType) { case KEY_EVENT: switch(inp.Event.KeyEvent.wVirtualKeyCode) { case 0x57: std::cout << "W\n"; break; case 0x41: std::cout << "A\n"; break; case 0x53: std::cout << "S\n"; break; case 0x44: std::cout << "D\n"; break; case VK_SPACE: std::cout << "Space\n"; break; case VK_F1: std::cout << "Help!\n"; break; case VK_ESCAPE: exit = true; break; } break; } }while(!exit); } |
We should note that you can also use the GetKeyState()
method to check specific key checks like we show below.
1 2 3 4 5 6 |
if( GetKeyState('A') & 0x8000 ) // check keypress A and if high-order bit is set (1 << 15) { } |
In C++ Builder, If you are developing visual VCL or FMX applications, handling key and mouse events are very easy in both VCL and FMX applications. They allow you to check if a Key is pressed down or released, You just need to use OnKeyDown
, OnKeyUp
events of a Form or particularly any component part on that form.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition