How to: Detect Whether a Gamepad Button Is Pressed

This topic describes how to determine whether a button is pressed, by using the A button on the Xbox One gamepad as an example. It assumes that you are familiar with how to use the gamepad API as described in Getting Started with the Gamepad API.

To detect whether the A button is pressed:

  1. Use Gamepad.Gamepads to get all connected gamepads.

    C++

    auto gamepads = Gamepad::Gamepads;  
    
  2. Use GetCurrentReading to get the gamepad reading object.

    C++

    // For simplicity, we assume there is only one connected Xbox One gamepad.
    // Otherwise, check if the gamepads vector size is > 0 first, by using this: if (gamepads->Size > 0)
    IGamepad^ gamepad0 = gamepads->GetAt(0);    
    IGamepadReading^ reading = gamepads->GetAt(0)->GetCurrentReading();  
    
  3. Call IsAPressed to retrieve the status of the A button.

    C++

    if (reading->IsAPressed)
    {
        // Add game logic for the A button currently pressed.  
    }  
    

    Alternatively, you can use the Buttons property to check whether the A button is currently pressed, as shown below.

    C++

        // Note: reading->Buttons returns a bit field that can have multiple bits set.
        if( ( reading->Buttons & GamepadButtons::A ) == GamepadButtons::A )
        {
            // Add game logic for A button currently pressed.
        }  
    

To detect whether other controls are pressed

In the last step of the previous procedure, replace the IsAPressed with the corresponding property listed in the following table.

Control Name Property Name
B button IsBPressed
Back button IsBackPressed
D-pad down IsDPadDownPressed
D-pad left IsDPadLeftPressed
D-pad right IsDPadRightPressed
D-pad up IsDPadUpPressed
Left bumper IsLeftShoulderPressed
Left stick IsLeftThumbstickPressed
Right stick IsRightThumbstickPressed
Right bumper IsRightShoulderPressed
Start IsStartPressed
X IsXPressed
Y IsYPressed

See Also

Getting Started with the Gamepad API

How to: Activate the Gamepad Impulse Triggers

How to: Activate Controller Vibration

How to: Get the Pairing Between Bodies and Users by Using an Event-Driven Approach

Programming the Xbox One Gamepad Controller

Gamepad Vibration Overview