Gamepads

BeginnerProgrammer

Gamepads, such as the Xbox Elite Wireless Controller and the PS4 DualShock, are popular input devices for consoles and desktop.

Note

Xenko is currently optimized for the Xbox Elite gamepad. Other controllers work, but might have unexpected button mappings. Gamepad-specific features like the PS4 DualShock touchpad aren't supported.

Digital and analog buttons

  • Digital buttons have two states: up and down. The D-pad, Start, Back, Thumbstick (press), A, B, X and Y buttons are digital buttons.

  • Analog buttons return a value depending on how hard the user presses. The triggers are analog buttons, and return a value between 0 and 1. The thumbsticks are also analog, and return values between -1 and 1 on the X and Y axes.

The Xbox Elite controller buttons have the following names in Xenko:

Xbox gamepad

Handle gamepad input

Check that gamepads are connected

Before handling gamepad input:

Digital buttons

To query the states and state changes of digital gamepad buttons, on the GamePad object, call:

MethodFunctionality
IsButtonDown(IGamePadDevice, GamePadButton)Checks whether the button is in the down state.
IsButtonPressed(IGamePadDevice, GamePadButton)Checks whether the user has pressed the button since the previous update.
IsButtonReleased(IGamePadDevice, GamePadButton)Checks whether the user has released the button since the previous update.

Button (GamePadButton) is the gamepad button you want to check.

You can also get the state of digital buttons using GamePadState.Buttons.

Note

The GamePadState.Buttons field is a bitmask that uses binary system. Depending on the bitmask value, you can determine which buttons are up or down.

To get the gamepad state, use IGamePadDevice.State.

Analog buttons

To query values of analog buttons, first get the current state of gamepad using GetGamePadByIndex(index), where index (Integer) is the index of the gamepad you want to check.

Warning

The value returned by IGamePadDevice.State is the state of the gamepad at the current update. You can't reuse this value for the next updates. You have to query it again in every update.

To get trigger and thumbstick positions, use these GamePadState fields:

FieldDescription
GamePadState.LeftThumbLeft thumbstick X-axis/Y-axis value in the range [-1.0f, 1.0f] for both axes.
GamePadState.RightThumbRight thumbstick X-axis/Y-axis value in the range [-1.0f, 1.0f] for both axes.
GamePadState.LeftTriggerLeft trigger analog control value in the range [0, 1.0f] for a single axes.
GamePadState.RightTriggerRight trigger analog control value in the range [0, 1.0f] for a single axis.

Thumbsticks move along the X and Y axes. Their positions read as follows:

Query thumb positionQuery thumb position

Triggers move along the X axis. Their positions read as follows:

Query trigger position

Vibration

To set the gamepad vibration level, use IGamePadDevice.SetVibration.

Note

Xenko currently only supports vibration for Xbox gamepads.

Example code

  1. using Xenko.Core.Mathematics;
  2. using Xenko.Engine;
  3. public class TestScript : SyncScript
  4. {
  5. public override void Update()
  6. {
  7. //Check if a gamepad is connected
  8. if (Input.HasGamePad)
  9. {
  10. //Get the number of connected gamepads
  11. int gamepadCount = Input.GamePadCount;
  12. // Check each gamepad's status
  13. foreach(var gamepad in Input.GamePads)
  14. {
  15. // Get the analog thumbstick positions
  16. Vector2 speed = gamepad.State.LeftThumb;
  17. Vector2 direction = gamepad.State.RightThumb;
  18. // Get the digital buttons' status
  19. if (gamepad.IsButtonDown(GamePadButton.X))
  20. {
  21. // The action repeats for as long as the user holds the button down.
  22. // This is useful for continuous actions such as firing a machine gun.
  23. }
  24. if (gamepad.IsButtonPressed(GamePadButton.A))
  25. {
  26. // The action is triggered only once, even if the user holds the button down.
  27. // This is useful for one-time actions such as jumping.
  28. }
  29. }
  30. }
  31. }
  32. }

See also