Gamepad Class

Definition

Represents a gamepad.

public ref class Gamepad sealed
public ref class Gamepad sealed : IGameControllerBatteryInfo
class Gamepad sealed
class Gamepad sealed : IGameControllerBatteryInfo
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Static(typeof(Windows.Gaming.Input.IGamepadStatics), 65536, typeof(Windows.Foundation.UniversalApiContract))]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class Gamepad
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[Windows.Foundation.Metadata.Static(typeof(Windows.Gaming.Input.IGamepadStatics), 65536, "Windows.Foundation.UniversalApiContract")]
public sealed class Gamepad
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[Windows.Foundation.Metadata.Static(typeof(Windows.Gaming.Input.IGamepadStatics), 65536, "Windows.Foundation.UniversalApiContract")]
[Windows.Foundation.Metadata.DualApiPartition(version=167772160)]
public sealed class Gamepad
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
[Windows.Foundation.Metadata.Static(typeof(Windows.Gaming.Input.IGamepadStatics), 65536, "Windows.Foundation.UniversalApiContract")]
[Windows.Foundation.Metadata.DualApiPartition(version=167772160)]
[Windows.Foundation.Metadata.Static(typeof(Windows.Gaming.Input.IGamepadStatics2), 262144, "Windows.Foundation.UniversalApiContract")]
public sealed class Gamepad : IGameControllerBatteryInfo
Public NotInheritable Class Gamepad
Public NotInheritable Class Gamepad
Implements IGameControllerBatteryInfo
Inheritance
Gamepad
Attributes
Implements
<xref href="Windows.Gaming.Input.IGameController?alt=Windows.Gaming.Input.IGameController&text=IGameController" data-throw-if-not-resolved="True"/> <xref href="Windows.Gaming.Input.IGameControllerBatteryInfo?alt=Windows.Gaming.Input.IGameControllerBatteryInfo&text=IGameControllerBatteryInfo" data-throw-if-not-resolved="True"/>

Windows 10 requirements

Device family
Windows 10 (introduced in 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduced in v1.0)

Examples

The following code snippet shows how to loop through the Gamepad.Gamepads list and add each one to a vector. You'll need to put a lock on the vector, because things can change at any time (a controller might be disconnected or reconnected, for example).

#include <concrt.h>
#include <winrt/Windows.Gaming.Input.h>
using namespace winrt;
using namespace Windows::Gaming::Input;
...
std::vector<Gamepad> myGamepads;
concurrency::critical_section myLock{};

for (auto const& gamepad : Gamepad::Gamepads())
{
    // Test whether the gamepad is already in myGamepads; if it isn't, add it.
    concurrency::critical_section::scoped_lock lock{ myLock };
    auto it{ std::find(begin(myGamepads), end(myGamepads), gamepad) };

    if (it == end(myGamepads))
    {
        // This code assumes that you're interested in all gamepads.
        myGamepads.push_back(gamepad);
    }
}
auto myGamepads = ref new Vector<Gamepad^>();
critical_section myLock{};

for (auto gamepad : Gamepad::Gamepads)
{
    // Test whether the gamepad is already in myGamepads; if it isn't, add it.
    critical_section::scoped_lock lock{ myLock };
    auto it = std::find(begin(myGamepads), end(myGamepads), gamepad);

    if (it == end(myGamepads))
    {
        // This code assumes that you're interested in all gamepads.
        myGamepads->Append(gamepad);
    }
}

Remarks

Instances of the Gamepad class cannot be created directly; instead, instances of the Gamepad class are retrieved through the Gamepad.Gamepads property listing all connected gamepads or through the Gamepad.GamepadAdded event.

Supported Devices

Gamepad supports any Xbox One certified or Xbox 360 compatible gamepad.

Version history

Windows version SDK version Value added
1607 14393 GetButtonLabel
1703 15063 FromGameController
1703 15063 TryGetBatteryReport

Properties

Gamepads

The list of all connected gamepads.

Headset

The audio headset attached to the gamepad.

IsWireless

Gets a value that indicates the wireless state of the gamepad.

User

The user associated with the gamepad.

Vibration

Describes the gamepad motor speed.

Methods

FromGameController(IGameController)

Returns the given game controller as a gamepad.

GetButtonLabel(GamepadButtons)

Retrieves the button label for the specified button.

GetCurrentReading()

Gets a snapshot of the gamepad state.

TryGetBatteryReport()

Gets information about the gamepad's current battery state.

Events

GamepadAdded

Signals when a new gamepad is connected.

GamepadRemoved

Signals when a gamepad is disconnected.

HeadsetConnected

Signals when a headset is attached to the gamepad.

HeadsetDisconnected

Signals when a headset is disconnected from the gamepad.

UserChanged

Signals when the user associated with the gamepad has changed.

Applies to

See also