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
繼承
Object Platform::Object IInspectable Gamepad
屬性
實作

Windows 需求

裝置系列
Windows 10 (已於 10.0.10240.0 引進)
API contract
Windows.Foundation.UniversalApiContract (已於 v1.0 引進)

範例

下列代碼段示範如何迴圈查看 Gamepad.Gamepads 清單,並將每個代碼段新增至向量。 您必須將鎖定放在向量上,因為每當控制器中斷連線或重新連線 (時,都可以變更專案,例如) 。

#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);
    }
}

備註

無法直接建立 Gamepad 類別的實例;而是透過 Gamepad.Gamepads 屬性擷取 Gamepad 類別的實例,其中列出所有連接的遊戲板或透過 Gamepad.GamepadAdded 事件擷取。

支援的裝置

遊戲板 支援任何 GIP (遊戲輸入通訊協定) 或 XUSB 相容的遊戲板。

版本歷程記錄

Windows 版本 SDK 版本 新增值
1607 14393 GetButtonLabel
1703 15063 FromGameController
1703 15063 TryGetBatteryReport

屬性

Gamepads

所有已連線遊戲板的清單。

Headset

附加至遊戲板的音訊頭戴式裝置。

IsWireless

取得值,這個值表示遊戲板的無線狀態。

User

與遊戲板相關聯的使用者。

Vibration

描述遊戲板馬達速度。

方法

FromGameController(IGameController)

以遊戲板的形式傳回指定的遊戲控制器。

GetButtonLabel(GamepadButtons)

擷取指定按鈕的按鈕標籤。

GetCurrentReading()

取得遊戲板狀態的快照集。

TryGetBatteryReport()

取得遊戲板目前電池狀態的相關信息。

事件

GamepadAdded

當新的遊戲板連線時發出訊號。

GamepadRemoved

當遊戲板中斷連線時發出訊號。

HeadsetConnected

當頭戴式裝置附加至遊戲板時發出訊號。

HeadsetDisconnected

當頭戴式裝置與遊戲板中斷連線時發出訊號。

UserChanged

當使用者與遊戲板相關聯的用戶已變更時發出訊號。

適用於

另請參閱