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

# IGameInputDevice::GetXInputDeviceIndex

> 获取分配给输入设备的 XInput 用户索引（如果有）。

获取分配给输入设备的 XInput 用户索引（如果有）。

## 语法

```cpp theme={null}
bool GetXInputDeviceIndex(
    uint32_t* index
);
```

### 参数

*index*   \_Out\_\
类型：uint32\_t\*

接收设备的从零开始的 XInput 用户索引（0 到 3）。如果设备没有 XInput 用户索引，则设置为 0。

### 返回值

类型：bool

如果设备当前具有 XInput 用户索引，则为 `true`；否则为 `false`。

## 备注

XInput 用户索引是 XInput API 用于标识控制器的控制器槽位，通常对应于控制器上显示的玩家指示灯。使用此方法可以将 GameInput 设备与在 XInput 中由同一用户索引所指的控制器关联起来，例如在将游戏逐个控制器地从 XInput 迁移到 GameInput 时。

只有通过 XInput 公开的设备才具有用户索引。其他设备（例如键盘、鼠标和大多数 HID 游戏控制器）返回 `false`。聚合设备从不具有 XInput 用户索引。设备断开连接时，其用户索引会被释放，并且该方法返回 `false`。

与大多数设备属性不同，XInput 用户索引可以在设备连接期间更改。用户索引更改时，GameInput 会使用 [GameInputDeviceEvent](https://learn.microsoft.com/en-us/gaming/gdk/docs/reference/input/gameinput/enums/gameinputdeviceevent) 的 `GameInputDeviceEventXInputIndexUpdated` 值通知已注册的设备回调。在该回调中再次调用 `GetXInputDeviceIndex` 以获取新索引。

```cpp theme={null}
void CALLBACK OnDeviceEvent(
    GameInputCallbackToken callbackToken,
    void* context,
    GameInputDeviceEvent deviceEvent,
    IGameInputDevice* device) noexcept
{
    if (deviceEvent == GameInputDeviceEventXInputIndexUpdated)
    {
        uint32_t index = 0;
        if (device->GetXInputDeviceIndex(&index))
        {
            // The device is now assigned to XInput user index 'index'.
        }
        else
        {
            // The device no longer has an XInput user index.
        }
    }
}
```

## 要求

**头文件：** GameInput.h

**库：** gameinput.lib

**支持的平台：** Windows、XBOX One 系列主机和 XBOX Series 主机

## 另请参阅

[GameInput 概述](https://docs.xbox.com/zh-CN/build/core-features/common/input/overviews/input-overview.md)\
[IGameInputDevice](https://docs.xbox.com/zh-CN/reference/input/gameinput/interfaces/igameinputdevice/igameinputdevice.md)\
[GameInputDeviceEvent](https://learn.microsoft.com/en-us/gaming/gdk/docs/reference/input/gameinput/enums/gameinputdeviceevent)\
[IGameInput::RegisterDeviceCallback](https://docs.xbox.com/zh-CN/reference/input/gameinput/interfaces/igameinput/methods/igameinput_registerdevicecallback.md)

## 版本历史

| 版本 | 变更 |
| - | - |
| **v4** | 引入。 |


## Related topics

- [IGameInput::RegisterDeviceCallback](https://docs.xbox.com/zh-CN/reference/input/gameinput/interfaces/igameinput/methods/igameinput_registerdevicecallback.md)
- [IGameInputDevice（接口内容）](https://docs.xbox.com/zh-CN/reference/input/gameinput/interfaces/igameinputdevice/igameinputdevice.md)
- [IGameInputDevice::GetGamepadInfo](https://docs.xbox.com/zh-CN/reference/input/gameinput/interfaces/igameinputdevice/methods/igameinputdevice_getgamepadinfo.md)
- [IGameInputDevice::GetTouchSurfaceInfo](https://docs.xbox.com/zh-CN/reference/input/gameinput/interfaces/igameinputdevice/methods/igameinputdevice_gettouchsurfaceinfo.md)
- [IGameInputDevice::GetDeviceFamily](https://docs.xbox.com/zh-CN/reference/input/gameinput/interfaces/igameinputdevice/methods/igameinputdevice_getdevicefamily.md)
