Dela via


Gamepad Class

Definition

Represents a gamepad.

public ref class Gamepad sealed
public ref class Gamepad sealed : IGameControllerBatteryInfo
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class Gamepad final
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class Gamepad final : IGameControllerBatteryInfo
[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)]
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)]
public sealed class Gamepad : IGameControllerBatteryInfo
Public NotInheritable Class Gamepad
Public NotInheritable Class Gamepad
Implements IGameControllerBatteryInfo
Inheritance
Object Platform::Object IInspectable Gamepad
Attributes
Implements

Windows 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 GIP (Gaming Input Protocol) or XUSB 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