IGameInput::GetCurrentReading (v2)

从与调用方提供的筛选器相匹配的输入流检索最近的读取。

语法

HRESULT GetCurrentReading(
    GameInputKind inputKind,
    IGameInputDevice* device,
    IGameInputReading** reading
);

参数

inputKind _In_
类型:GameInputKind

枚举值之一,用于指定所使用的输入设备类型,例如控制器、键盘、鼠标或游戏手柄。 可以组合枚举值来指定多种输入类型。 在指定多种输入类型时,将匹配并返回包含至少一种输入类型的任何读取。

device _In_opt_
类型:IGameInputDevice*

可从特定设备返回读取信息的可选筛选器。

reading _COM_Outptr_
类型:IGameInputReading**

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

返回值

类型:HRESULT

函数结果。

备注

此函数用于最初访问输入流。 可将此函数与 GetNextReadingGetPreviousReading 方法结合使用,浏览输入流,并且可不丢失输入。 或者,如果游戏可以允许丢失某些输入,则可以继续调用 GetCurrentReading 以获取最新的读数。

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

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

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

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

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

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

要求

头文件:GameInput.h

库:gameinput.lib

支持的平台: 窗户

另请参阅

输入 API 概述IGameInput_GetNextReading IGameInput_GetPreviousReadingIGameInput