C++C++11C++14C++17Game DevelopmentIntroduction to C++Learn C++Syntax

Learn How To Use Keyboard Arrows In C++ On Windows

Learn How To Use Keyboard Arrows In C++ On Windows

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,

ConstantValueDescription
VK_LEFT0x25LEFT ARROW key
VK_UP0x26UP ARROW key
VK_RIGHT0x27RIGHT ARROW key
VK_DOWN0x28DOWN 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.

Learn How To Use Keyboard Arrows In C++ On Windows the C++ Builder Logo

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

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?