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,
for example we can use this as below,
where these parameters are defined before as given here;
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.
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:
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;
We should note that you can also use GetKeyState()
method to check specific key checks as below.
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