How To Use A Game Pad Or Joystick Controller In C++ On Windows
C++ On Windows is one of the most powerful programming languages that we use for all sorts of purposes, from regular applications, games, business, industrial infrastructure, robotics, and in the control of IoT devices. The most well-known controllers for those areas where human and computer interaction are important and stretches beyond simple keyboard input are joysticks or gamepads. One of the simplest examples to use them on Windows is using the venerable XInput library which has been around for quite a long time but can still be easily used with the latest C++ Compiler. In this post, we explain how you can use a gamepad or joystick controller in C++ On Windows with Xinput library. Is there a component for the gamepad or joystick in C++ On Windows? If you are looking for a ready-to-use component library, there are Delphi and C++ Builder-compatible components built on the XInput library. For example, the Controller library is a Delphi and C++ Builder component that allows applications to receive input from an 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 Winsoft.sk. How can I control a gamepad or joystick in C++ with the Xinput Library? The XInput library is deprecated but it still supported 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: XInput library is fairly old and easy to implement Windows applications. XInput library is limited to 4 controllers. XInput library has no uniform interface to other inputs (like mouse/keyboard). XInput library input occurs with higher latency. XInput library is not friendly with other controllers. i.e. no support for Xbox One Rumble Motors. So, briefly, DirectInput is a better long-term choice than XInput. If you still want to use XInput, you can read more about https://learn.microsoft.com/en-us/windows/win32/xinput/getting-started-with-xinput XINPUT_GAMEPAD structure (declared in ) is explained here : https://learn.microsoft.com/en-us/windows/win32/api/xinput/ns-xinput-xinput_gamepad 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, wButtons Device digital button flags Gamepad Bitmask XINPUT_GAMEPAD_DPAD_UP 0x0001 XINPUT_GAMEPAD_DPAD_DOWN 0x0002 XINPUT_GAMEPAD_DPAD_LEFT 0x0004 XINPUT_GAMEPAD_DPAD_RIGHT 0x0008 XINPUT_GAMEPAD_START 0x0010 XINPUT_GAMEPAD_BACK 0x0020 XINPUT_GAMEPAD_LEFT_THUMB 0x0040 XINPUT_GAMEPAD_RIGHT_THUMB 0x0080 XINPUT_GAMEPAD_LEFT_SHOULDER 0x0100 XINPUT_GAMEPAD_RIGHT_SHOULDER 0x0200 XINPUT_GAMEPAD_A 0x1000 XINPUT_GAMEPAD_B 0x2000 XINPUT_GAMEPAD_X 0x4000 XINPUT_GAMEPAD_Y 0x8000 How to use a gamepad or joystick in C++? If you wonder how XInput library works, you can create a simple example as in given steps below. First, we need library header to use XInput library. This is how you can use XInput library in C++. #include #include #include #pragma comment(lib, “XInput.lib”) Now, lets create a TController class that have n for the controller number and state for it. Here is an example. class TController { private: int n; XINPUT_STATE state; public: // add controller methods here }; Then we need to add some methods for this class, first lets set our private controller number n in construction like […]
