If you want to develop console applications and you need to perform some actions in accordance with key presses such as those you find in games, you need to handle these inputs in your applications. Many game engines are based on C++ and while it’s a lot more complicated to create a full C++ Game engine you will find an IDE like C++ Builder supports event handling just fine, and you can easily create 2D and 3D applications with the UI libraries it provides. Actually, there are many ways to detect key inputs. In this article, we explain just one of the ways that you can detect key events in C++ on Windows, specifically with an example that shows how you can use keyboard arrows in C++ on Windows.
How to detect key presses and use keyboard arrows in C++ on Windows?
There are many ways to detect key presses in C++. Key input detection may depend on device architecture, your PC hardware, and the capabilities of the operating system. Here, we will explain how you can use it on Windows console applications in C++, C++ Builder, Dev-C++ and other similar 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 keys and 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
If you want to get key presses in console applications on Windows, you can use ReadConsoleInput
method.
Here is the Syntax for the 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 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; |
To detect arrow keys, we can get Virtual Key Code with inp.Event.KeyEvent.wVirtualKeyCode
then we can check this code with one of these codes below,
Constant | Value | Description |
VK_LEFT | 0x25 | LEFT ARROW key |
VK_UP | 0x26 | UP ARROW key |
VK_RIGHT | 0x27 | RIGHT ARROW key |
VK_DOWN | 0x28 | DOWN ARROW key |
For more Windows keys and their decimal values, please check out the Microsoft page here: https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
Is there a simple example of how to detect key presses and keyboard arrows in C++ on Windows?
In an example, we can use ReadConsoleInput()
method to read inp
input, then we can get Virtual Key Code with inp.Event.KeyEvent.wVirtualKeyCode
as shown below.
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) { } } |
In the following example, if you want to detect pressed return characters like ‘a’, ‘s’, ‘d’, you can use inp.Event.KeyEvent.uChar.AsciiChar
like this:
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) { } } |
Is there a full example of how to detect and use keyboard arrows in C++ on Windows?
Here is a full C++ example that detects arrow keys (VK_LEFT, VK_RIGHT, VK_UP, VK_DOWN) , the space key, F1 key and the 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 VK_UP: std::cout << "UP\n"; break; case VK_DOWN: std::cout << "DOWN\n"; break; case VK_LEFT: std::cout << "LEFT\n"; break; case VK_RIGHT: std::cout << "RIGHT\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 GetKeyState()
method to check specific key checks as 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