> ## 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::GetControllerSwitchInfo

> 检索有关设备上通用控制器开关（例如帽子开关和方向键）的信息。

检索有关设备上通用控制器开关（例如帽子开关和方向键）的信息。

## 语法

```cpp theme={null}
uint32_t GetControllerSwitchInfo(
    uint32_t infoArrayCount,
    GameInputControllerSwitchInfo* infoArray
);
```

### 参数

*infoArrayCount*   \_In\_\
类型：uint32\_t

*infoArray* 中的元素数量。若要检索所有控制器开关，请传递 [GetControllerSwitchCount](https://docs.xbox.com/zh-CN/reference/input/gameinput/interfaces/igameinputdevice/methods/igameinputdevice_getcontrollerswitchcount.md) 返回的值。

*infoArray*   \_Out\_writes\_to\_opt\_(infoArrayCount, return)\
类型：[GameInputControllerSwitchInfo](https://docs.xbox.com/zh-CN/reference/input/gameinput/structs/gameinputcontrollerswitchinfo.md)\*

指向数组的指针，该数组为每个控制器开关接收一个结构。可以为 `nullptr`，在这种情况下不会写入任何内容，并且方法返回 0。

### 返回值

类型：uint32\_t

写入 *infoArray* 的元素数量。该值为 *infoArrayCount* 与设备上控制器开关数量中的较小者；如果 *infoArray* 为 `nullptr` 或设备没有控制器开关，则为 0。

## 备注

每个元素都是一个 [GameInputControllerSwitchInfo](https://docs.xbox.com/zh-CN/reference/input/gameinput/structs/gameinputcontrollerswitchinfo.md) 结构，用于描述相应开关的类型（2 向、4 向或 8 向，以 [GameInputSwitchKind](https://docs.xbox.com/zh-CN/reference/input/gameinput/enums/gameinputswitchkind.md) 值表示）及其每个位置的标签。

调用 [GetControllerSwitchCount](https://docs.xbox.com/zh-CN/reference/input/gameinput/interfaces/igameinputdevice/methods/igameinputdevice_getcontrollerswitchcount.md) 获取控制器开关的数量，分配该大小的数组，然后调用 `GetControllerSwitchInfo` 填充该数组。如果 *infoArrayCount* 小于控制器开关的数量，则只写入前 *infoArrayCount* 个元素。在设备保持连接期间，设备的控制器开关不会更改，因此你可以只查询一次并缓存结果。

信息数组的元素 *i* 描述的是，对于来自此设备的读数，[IGameInputReading::GetControllerSwitchState](https://docs.xbox.com/zh-CN/reference/input/gameinput/interfaces/igameinputreading/methods/igameinputreading_getcontrollerswitchstate.md) 返回的数组中索引 *i* 处的开关。

以下示例演示如何检索设备的控制器开关。

```cpp theme={null}
uint32_t switchCount = device->GetControllerSwitchCount();
std::vector<GameInputControllerSwitchInfo> switchInfo(switchCount);
switchInfo.resize(device->GetControllerSwitchInfo(switchCount, switchInfo.data()));

std::vector<GameInputSwitchPosition> switchStates(reading->GetControllerSwitchCount());
reading->GetControllerSwitchState(static_cast<uint32_t>(switchStates.size()), switchStates.data());

for (size_t i = 0; i < switchInfo.size() && i < switchStates.size(); ++i)
{
    // switchInfo[i] describes the switch whose position is switchStates[i].
}
```

在 GameInput v3 及更早版本中，此信息可从 [GameInputControllerInfo](https://docs.xbox.com/zh-CN/reference/input/gameinput/deprecated/structs/gameinputcontrollerinfo.md) 结构的 `controllerSwitchInfo` 成员中获取，该结构由 [IGameInputDevice::GetDeviceInfo](https://docs.xbox.com/zh-CN/reference/input/gameinput/deprecated/interfaces/igameinputdevice/methods/igameinputdevice_getdeviceinfo.md) 返回的 [GameInputDeviceInfo](https://docs.xbox.com/zh-CN/reference/input/gameinput/deprecated/structs/gameinputdeviceinfo.md) 结构的 `controllerInfo` 成员引用。

## 要求

**头文件：** 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)\
[GetControllerSwitchCount](https://docs.xbox.com/zh-CN/reference/input/gameinput/interfaces/igameinputdevice/methods/igameinputdevice_getcontrollerswitchcount.md)\
[GameInputControllerSwitchInfo](https://docs.xbox.com/zh-CN/reference/input/gameinput/structs/gameinputcontrollerswitchinfo.md)\
[GameInputSwitchKind](https://docs.xbox.com/zh-CN/reference/input/gameinput/enums/gameinputswitchkind.md)\
[IGameInputReading::GetControllerSwitchState](https://docs.xbox.com/zh-CN/reference/input/gameinput/interfaces/igameinputreading/methods/igameinputreading_getcontrollerswitchstate.md)\
[IGameInputDevice](https://docs.xbox.com/zh-CN/reference/input/gameinput/interfaces/igameinputdevice/igameinputdevice.md)

## 版本历史

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


## Related topics

- [GameInputControllerSwitchInfo](https://docs.xbox.com/zh-CN/reference/input/gameinput/structs/gameinputcontrollerswitchinfo.md)
- [IGameInputDevice::GetControllerSwitchCount](https://docs.xbox.com/zh-CN/reference/input/gameinput/interfaces/igameinputdevice/methods/igameinputdevice_getcontrollerswitchcount.md)
- [IGameInputDevice::GetControllerAxisCount](https://docs.xbox.com/zh-CN/reference/input/gameinput/interfaces/igameinputdevice/methods/igameinputdevice_getcontrolleraxiscount.md)
- [IGameInputDevice::GetControllerButtonCount](https://docs.xbox.com/zh-CN/reference/input/gameinput/interfaces/igameinputdevice/methods/igameinputdevice_getcontrollerbuttoncount.md)
- [IGameInputDevice::GetControllerAxisLabels](https://docs.xbox.com/zh-CN/reference/input/gameinput/interfaces/igameinputdevice/methods/igameinputdevice_getcontrolleraxislabels.md)
