> ## 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.

# IGameInput::GetCurrentReading

> IGameInput::GetCurrentReading

# IGameInput::GetCurrentReading

从输入流中检索与调用方提供的筛选器匹配的最新读数。

## 语法

```cpp theme={null}
HRESULT GetCurrentReading(
    GameInputKind inputKind,
    IGameInputDevice* device,
    IGameInputReading** reading
);
```

### 参数

*inputKind*   \_In\_\
类型：[GameInputKind](/reference/input/gameinput/enums/gameinputkind)

指示正在使用的输入设备类型（例如控制器、键盘、鼠标或游戏手柄）的枚举值之一。可以组合多个枚举值以指示多种输入类型。指定多种输入类型时，任何至少包含其中一种输入类型的读数都会被匹配并返回。

*device*   \_In\_opt\_\
类型：IGameInputDevice\*

一个可选的筛选器，用于返回特定设备的读数。

*reading*   \_COM\_Outptr\_\
类型：IGameInputReading\*\*

要返回的输入读数。失败时返回 `NULL`。

### 返回值

类型：HRESULT

函数结果。

## 备注

此函数用于初次访问输入流。可以将此函数与 `GetNextReading` 和 `GetPreviousReading` 方法一起使用，以逐个读数遍历输入流而不遗漏任何输入。或者，如果你的游戏可以允许错过一些输入，你可以继续调用 `GetCurrentReading` 以持续获取最新的读数。

以下代码示例演示了如何轮询当前游戏手柄状态。

```cpp theme={null}
Microsoft::WRL::ComPtr<IGameInput> gameInput;

void PollGamepadInput() noexcept
{
    Microsoft::WRL::ComPtr<IGameInputReading> reading;

    if (SUCCEEDED(gameInput->GetCurrentReading(
        GameInputKindGamepad,
        nullptr,
        &reading)))
    {
        // Application-specific code to process the reading.
    }
}
```

以下代码示例演示了如何从特定设备轮询当前游戏手柄状态。

```cpp theme={null}
Microsoft::WRL::ComPtr<IGameInput> gameInput;
Microsoft::WRL::ComPtr<IGameInputDevice> gamepad;

void PollGamepadInput() noexcept
{
    Microsoft::WRL::ComPtr<IGameInputReading> reading;

    if (SUCCEEDED(gameInput->GetCurrentReading(
        GameInputKindGamepad,
        gamepad.Get(),
        &reading)))
    {
        // Lock onto the first device we find input from, since this
        // must be the one the player is using (if it's generating input).
        if (!gamepad)
        {
            reading->GetDevice(&gamepad);
        }

        // Application-specific code to process the reading.
    }

    else
    {
        // Go back to looking for a device to lock onto, if the previous one is gone.
        gamepad = nullptr;
    }
}
```

以下代码示例演示了如何从特定设备轮询所有游戏手柄状态。

```cpp theme={null}
Microsoft::WRL::ComPtr<IGameInput> gameInput;
Microsoft::WRL::ComPtr<IGameInputDevice> gamepad;
Microsoft::WRL::ComPtr<IGameInputReading> prevReading;

void PollGamepadInput() noexcept
{
    if (!prevReading)
    {
        if (SUCCEEDED(gameInput->GetCurrentReading(
            GameInputKindGamepad,
            nullptr,
            &prevReading)))
        {
            gamepad.Attach(prevReading->GetDevice());

            // Application-specific code to process the initial reading.
        }
    }

    else
    {
        Microsoft::WRL::ComPtr<IGameInputReading> nextReading;
        HRESULT hr = gameInput->GetNextReading(
            prevReading.Get(),
            GameInputKindGamepad,
            gamepad.Get(),
            &nextReading);

        if (SUCCEEDED(hr))
        {
            // Application-specific code to process the next reading.

            prevReading = nextReading;
        }

        else if (hr != GAMEINPUT_E_READING_NOT_FOUND)
        {
            gamepad = nullptr;
            prevReading = nullptr;
        }
    }
}
```

## 要求

[输入 API 概述](/build/core-features/common/input/overviews/input-overview)\
[IGameInput\_GetNextReading](/reference/input/gameinput/interfaces/igameinput/methods/igameinput_getnextreading)\
[IGameInput\_GetPreviousReading](/reference/input/gameinput/interfaces/igameinput/methods/igameinput_getpreviousreading)\
[IGameInput](/reference/input/gameinput/interfaces/igameinput/igameinput)

## 版本历史

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


## Related topics

- [IGameInput::GetTemporalReading](/zh-CN/reference/input/gameinput/deprecated/interfaces/igameinput/methods/igameinput_gettemporalreading.md)
- [IGameInput::GetPreviousReading](/zh-CN/reference/input/gameinput/interfaces/igameinput/methods/igameinput_getpreviousreading.md)
- [IGameInput::GetNextReading](/zh-CN/reference/input/gameinput/interfaces/igameinput/methods/igameinput_getnextreading.md)
- [IGameInput::FindDeviceFromId](/zh-CN/reference/input/gameinput/interfaces/igameinput/methods/igameinput_finddevicefromid.md)
- [GameInputKind](/zh-CN/reference/input/gameinput/enums/gameinputkind.md)
