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

Learn How To Detect And Use Key Presses In C++ On Windows

Learn How To Detect And Use Key Presses In C++ On Windows

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.

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,

for example we can use this as we show below.

Where these parameters are defined before as given here;

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.

As in this example, if you want to detect characters like ‘a’, ‘s’, ‘d’, you can use inp.Event.KeyEvent.uChar.AsciiChar like so:

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;

We should note that you can also use the GetKeyState() method to check specific key checks like we show 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 Detect And Use Key Presses 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.

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++17Language FeatureLearn C++

How To Use The SVG Image Format With Skia In C++ Builder

C++C++17Language FeatureLearn C++

How To Use Skia Images in C++ Builder?

C++C++17Code SnippetGame DevelopmentLanguage FeatureLearn C++

What Is Skia In Modern C++?

C++C++17Learn C++

How To Use Skia in C++ Builder 12?