C++C++17ComponentsGame DevelopmentLanguage FeatureLearn C++

How To Use Gamepad Or Joystick Controller In A C++ Builder VCL App?

How To Use Gamepad Or Joystick Controller In A C++ Builder VCL App

C++ is one of the most powerful programming languages that we can use to create games that are playable with mouse, keyboard and gamepads or joysticks. We use C++ not just in games but also in almost every kind of industry, in robotics, and in the control of IoT devices. Games frequently have better playability if the user can control the game with input devices other than keyboard. The most popular controllers for games are gamepads and joysticks. One of the simplest ways to use such game controller hardware on Windows is using the venerable XInput library. Despite XInput having been around for a long time it can still be used with the latest C++ IDE and compiler for Windows. In this post, we explain how you can use gamepad or joystick controller in C++ using with Xinput library.

Is there a ready-made component for controlling a gamepad or joystick in C++ on Windows?

If you are looking for a component library, there are Delphi and C++ Builder-compatible components built around the XInput library. The Controller library is a Delphi and C++ Builder component that allows applications to receive input from the Xbox Controller. The main features of this library are,

  • Uses Windows XInput API.
  • Available for Delphi/C++ Builder 6 – 11 and Lazarus 2.0.12
  • Source code included in the registered version.
  • Royalty free distribution.

You can download the trial version of the XInput library, or you can buy a professional edition from here.

How can I control a gamepad or joystick in C++ with the Xinput Library?

XInput library was developed quite some time ago and is a deprecated but still supported library by Microsoft. They recommend moving towards the GameInput library or Windows.Game.Input for Windows applications.

If we look at these kinds of libraries (i.e GamePad.h library), like MS’ own DirectXTK, we can see that the toolkit allows one to define USING_XINPUT vs. USING_GAMEINPUT vs. USING_WINDOWS_GAMING_INPUT to pick which underlying library is used.

If we compare  XInput compared to GameInput:

  1. XInput library is easy to implement Windows applications.
  2. XInput library is limited to 4 controllers.
  3. XInput library has no uniform interface to other input (like mouse/keyboard).
  4. XInput library occurs with higher latency.
  5. XInput library is not friendly with other controllers. i.e. no support for Xbox One Rumble Motors.

In brief, DirectInput is better than XInput. If you still want to use XInput, you can check more about https://learn.microsoft.com/en-us/windows/win32/xinput/getting-started-with-xinput

XINPUT_GAMEPAD structure (declared in <xinput.h>) is explained here: https://learn.microsoft.com/en-us/windows/win32/api/xinput/ns-xinput-xinput_gamepad

In there, this structure has these members: wButtons, bLeftTrigger, bRightTrigger, sThumbLX, sThumbLY, sThumbRX, sThumbRY. Here wButtons member is used as a bitmask of the device digital buttons, it can be used as below.

wButtons
Device digital button flags
Gamepad
Bitmask
XINPUT_GAMEPAD_DPAD_UP0x0001
XINPUT_GAMEPAD_DPAD_DOWN0x0002
XINPUT_GAMEPAD_DPAD_LEFT0x0004
XINPUT_GAMEPAD_DPAD_RIGHT0x0008
XINPUT_GAMEPAD_START0x0010
XINPUT_GAMEPAD_BACK0x0020
XINPUT_GAMEPAD_LEFT_THUMB0x0040
XINPUT_GAMEPAD_RIGHT_THUMB0x0080
XINPUT_GAMEPAD_LEFT_SHOULDER0x0100
XINPUT_GAMEPAD_RIGHT_SHOULDER0x0200
XINPUT_GAMEPAD_A0x1000
XINPUT_GAMEPAD_B0x2000
XINPUT_GAMEPAD_X0x4000
XINPUT_GAMEPAD_Y0x8000

How to use a gamepad or joystick in C++ Builder VCL Application?

Here are the step to use gamepad or joystick in C++ Builder VCL app,

  1. Create a new VCL Windows application in C++ Builder.
  2. Drag a Timer (TTimer) component onto the Form.
  3. In the Object Inspector window, set its interval to 15 and be sure it is enabled.
  4. Drag a Shape (TShape) component on to Form.
  5. In Object Inspector window, set its Shape property to stCircle, change Brush color something that makes you happy.
  6. Double click to Timer1 to create Timer1Timer().
How To Use Gamepad Or Joystick Controller In A C++ Builder VCL App The demo app in the C++ Builder IDE

Now we can add the XInput library and a custom TController class as shown below:

In the Timer1Timer() procedure we can check if our controller is connected, and if it is true, then we can check state of wButtons. For example, for the A button we can check it with the XINPUT_GAMEPAD_A flag. If this condition is provided, then we can move our shape (for example we can increase the Top value) and we can vibrate it like we show in the example below.

Here we can use any device digital button flags as given in the table above. Note that, we should disable Timer1 and delete our controller0 when the Form is destroyed. So, in the Object Inspector, double click the OnDestroy() event of the Form and write these lines:

Now, let’s see a full VCL example in C++ Builder.

Is there a full C++ Builder VCL example about to use a gamepad or joystick in C++?

The example above works well with my son’s Logitech F710 Wireless on Windows Console application in C++ Builder. This controller class also works with FMX app and VCL apps too. I think, this example is also compatible with the XBOX series too. Please let me know if anyone tested on an XBOX. Here is a full example that uses the XInput library in C++.

Note that you can declare this TController class under the TForm1 class in header.

How To Use Gamepad Or Joystick Controller In A C++ Builder VCL App 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 version.

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
Artificial Intelligence TechC++C++17C++20Learn C++

How To Create Simple Generative AI Code In C++

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++?