1
\$\begingroup\$

I need some help here. I try this for a while now but it won't work and I can't find anything online. I want to write my own input handling. At first I used XInput for gamepads and Win32 callbacks for Keyboard input. That worked so far. Then I stumbled across GameInput and it seemed to me like this is a more modern approach so I tried it. For Gamepads (at least the ones I got here to test) it works like a charm. Then I tried to get it to work for keayboard as well, but it just won't recognize any keystrokes.

I put together this minimum example after https://learn.microsoft.com/en-us/gaming/gdk/_content/gc/input/overviews/input-readings#a-simple-gamepad-input-loop

#include <GameInput.h>
#include <iostream>
#include <thread>

IGameInput* g_gameInput = nullptr;
IGameInputDevice* g_device = nullptr;

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

void ShutdownInput()
{
    if (g_device) g_device->Release();
    if (g_gameInput) g_gameInput->Release();
}

bool PollGamepadInput()
{
    IGameInputReading* reading;
    if (SUCCEEDED(g_gameInput->GetCurrentReading(GameInputKindKeyboard, g_device, &reading)))
    {
        if (!g_device)
        {
            reading->GetDevice(&g_device);

            auto info = g_device->GetDeviceInfo();
            std::cout << "Family: " << info->deviceFamily << std::endl;
            std::cout << "Capabilities: " << info->capabilities << std::endl;
            std::cout << "Vendor: " << info->vendorId << std::endl;
            std::cout << "Product: " << info->productId << std::endl;
        
            if(info->displayName)
                std::cout << "DisplayName: " << info->displayName->data << std::endl;
        }

        auto count = reading->GetKeyCount();

        if (count > 0)
            std::cout << "Keys: " << count << std::endl;

        reading->Release();
        return true;
    }

    if (g_device)
    {
        g_device->Release();
        g_device = nullptr;
    }

    return true;
}

int main()
{
    if (InitializeInput() != S_OK)
        return -1;

    while (PollGamepadInput())
        std::this_thread::sleep_for(std::chrono::milliseconds(1));

    ShutdownInput();
    return 0;
}

but I get no key strokes. When I use GameInputKindGamepad as a filter type a can just use getGamepadState and it just works. Unfortunately there is no such thing as a getKeyboardState.

GameInput seems like a rather new thing, but the documentation states nothing about "Keyboards does not work yet". Does anyone have an idea on what I'm missing here?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Keystrokes are handled differently. It's discussed in the documentation under one of the advanced topics:

The GetKeyState method retrieves scan code and virtual key data for each pressed key on a keyboard. This input data is then returned as a variable-length array of GameInputKeyState structs. A pressed key is one that's held down at the time that the keyboard reported an input. Keyboards vary in how frequently they report input, typically ranging from 125 Hz to 1,000 Hz. Keyboard state is reported separately for each individual keyboard that's connected to the system.

A word of caution: the docs go on to say:

Keyboard state is reported separately for each individual keyboard that's connected to the system. The CreateAggregateDevice method on the IGameInput interface is used to get the combined system keyboard state.

However up at the top of the same reference, at the time of posting this, there's a note stating that CreateAggregateDevice isn't supported at this time.

\$\endgroup\$
2
  • \$\begingroup\$ But this just states that 'CreateAggregateDevice' is just usefull if a have multiple keyboards. and with this I just could combine them all to use like a single device. \$\endgroup\$ Commented Sep 25, 2023 at 7:15
  • \$\begingroup\$ I mentioned it because the order of information in the docs creates the strong potential for problems. They recommend CreateAggregateDevice in a couple of places, but it's not currently supported & the notice about that isn't near the keyboard related recommendation to use it. If their contradictory information isn't relevant to your code, it's fine to ignore it. \$\endgroup\$ Commented Sep 25, 2023 at 16:23

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.