> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 从 Windows.XBOX.Input 移植到 GameInput

> 从 Windows.XBOX.Input 移植到 GameInput

<a id="introductionSection" />

`Windows.Xbox.Input` 是用于 GDK 之前的 XBOX One 软件开发工具包开发的输入 API。从概念上讲，`GameInput` 看待设备和输入读取的方式与之不同。本主题介绍 `Windows.Xbox.Input` 与 `GameInput` 之间的一些关键差异。

## 以输入为中心与以设备为中心

`Windows.Xbox.Input` 与 `GameInput` 的关键区别在于，前者要求你从收集设备开始，并将输入状态作为设备的属性；相比之下，`GameInput` 从可筛选的输入读取流开始，读取会关联相应设备。

## 检测 A 按键按下

游戏启动时通常需要用户按 **A** 表示准备好继续。这里通过示例比较在 `Windows.Xbox.Input` 和 `GameInput` 中如何实现。此示例仅检测游戏手柄。请注意，示例经过简化以便演示。

### Windows.Xbox.Input

以下代码示例先枚举连接到系统的所有游戏手柄并将它们存储在 `gamepads` 向量中，然后在循环中遍历 `gamepads`，查看它们各自的当前输入读取，并检查每次读取中的 **A** 键。

<Note>此代码引用的是较早的 API。这些调用在 `GameInput` 中不存在。</Note>

```c++ theme={null}
void PollGamepadInput()
{
    // Find all gamepads.
    IVectorView<IGamepad^>^ gamepads = Gamepad::GetGamepads();

    // Cycle through each gamepad.
    for ( unsigned int i = 0; i < gamepads->Size; ++i )
    {
        // Get the gamepad's current reading.
        IGamepadReading^ reading = gamepads->GetAt(i)->GetCurrentReading();

        if ( reading->IsAPressed )
        {
            // Logic for the A button being pressed.
        }
    }
}
```

此模型可以改进。游戏必须存储连接到系统的不同设备的副本。你必须了解每个设备的具体信息。此外，通过存储所有游戏手柄的本地副本，代码捕获的是某一时刻的状态，而非获取最新的读取。

### GameInput

在以下代码示例中，代码查找来自任何设备的最新游戏手柄读取。通过调用 [GetCurrentReading](/reference/input/gameinput/interfaces/igameinput/methods/igameinput_getcurrentreading) 并将 [GameInputKind](/reference/input/gameinput/enums/gameinputkind) 筛选器设置为 `GamePad`，代码请求 `m_gameInput` 只返回游戏手柄读取。然后代码查询读取的状态并检查 **A** 键是否按下。

你可能希望跟踪自己在与哪个游戏手柄通信。有关更完整的示例，请参阅 [GameInput 读取](/build/core-features/common/input/overviews/input-readings)。

```c++ theme={null}
IGameInput* g_gameInput = nullptr;

HRESULT InitializeInput()
{
    return GameInputCreate(&g_gameInput);
}

void PollGamepadInput()
{
    ComPtr<IGameInputReading> reading;

    // Get the most current gamepad reading, not filtering by a specific device.
    // An error from the GetReading method indicates that no gamepad readings
    // were found.
    if (SUCCEEDED(g_gameInput->GetCurrentReading(GameInputKindGamepad, nullptr, &reading)))
    {
        // Read the gamepad state.
        GameInputGamepadState state;
        reading->GetGamepadState(&state);

        if (state.buttons & GameInputGamepadA)
        {
            // Logic for the A button being pressed.
        }
    }
}
```

<Note>`GameInput` 不要求你遍历设备并检查每个读取。相反，你可以通过一次方法调用获取最新的输入状态。你可以自行选择要了解每个物理设备的多少信息。</Note>

<a id="seeAlsoSection" />

## 参考 API 文档

* [GameInput（API 目录）](/reference/input/gameinput/gameinput_members)

## 另请参阅

[GameInput 概述](/build/core-features/common/input/overviews/input-overview)

[输入 API 参考](/reference/input/gc-reference-input-toc)

[Microsoft Game Development Kit](/services/playfab/sdks/platforms/gdk)


## Related topics

- [将 GameInput 与现有输入堆栈结合](/zh-CN/build/core-features/common/input/porting/input-porting.md)
- [将现有输入代码移植到 GameInput](/zh-CN/build/core-features/common/input/porting/index.md)
- [从 XInput 移植到 GameInput](/zh-CN/build/core-features/common/input/porting/input-porting-xinput.md)
- [面向 XBOX GDK 的移植指南](/zh-CN/home/build-first-title/porting-guides.md)
- [GameInput 常见问题](/zh-CN/build/core-features/common/input/overviews/input-faq.md)
