How To Use Gamepad Or Joystick Controller In A C++ Builder FMX 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 also use C++ in commerce, industry, in robotics, and in the control of IoT devices. Despite competition from other programming languages the games market is still very much dominated by C++. Many games seek to go beyond keyboards to control the gameplay by using specialized controllers either in addition to or instead of keypresses on the computer’s keys. Two of the most well-known controllers in these areas are joysticks or gamepads. The simplest examples of interfacing with game controllers on Windows is by using the XInput library. Even though XInput has been around for a long time it can still be used with the latest C++ Editor 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 component for using 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 on 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 and C++ Builder from versions 6 right up to the current modern version 11.
  • 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 Xinput Library?

XInput library is deprecated but is still a 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 old and 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.

Briefly, DirectInput is better than XInput. If you still want to use XInput, you can check more about

XINPUT_GAMEPAD structure (declared in ) is explained here :

As 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,

Device digital button flags

How to use a gamepad or joystick in a C++ Builder FMX application?

Here are the steps to use gamepad or joystick in C++ Builder FMX app,

  1. Create a new Multi-Device C++ Builder FMX application
  2. Drag a Timer (TTimer) component on to Form
    In Object Inspector window, set its interval to 15 and be sure it is enabled
  3. Drag a shape Circle (TCircle) component on to Form
    In Object Inspector window, change Fill color something that makes you happy
  4. Double click to Timer1 to create Timer1Timer()
How To Use Gamepad Or Joystick Controller In A C++ Builder FMX App? The screen in the form design view of RAD Studio

Now we can add XInput library and a custom TController class as below,

In Timer1Timer() procedure now 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 XINPUT_GAMEPAD_A flag. If this condition is provided, then we can move our shape Circle1 (for example we can increase Top value) and we can vibrate it as in 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 Object Inspector, double click the OnDestroy() event of Form and write these lines below,

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

Is there a full C++ Builder FMX example of how 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 works with an FMX app and VCL apps too. I think this example is compatible with XBOX series as well. Please let me know if anyone tested on an XBOX. Here is a full FMX example that uses XInput library in C++.

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

How To Change The Background Color Of A Memo In An FMX C++ 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.