Question

I'm trying to use the SDL2-CS binding and ran into a problem with SDL_GetKeyboardState(). Maybe I'm doing something wrong with the way I access the returned array, but it seems like the data inside is wrong.

Here's a test I did:

SDL.SDL_PumpEvents();
states = SDL.SDL_GetKeyboardState( out arraySize );

unsafe 
{
    uint* s = (uint*)states;
    for (int i=0; i<arraySize; ++i)
    {
        if (s[i] > 0 && i < 100)
        {
            Debugger.Break();
        }
    }
}

When I press 'Q', it stop at i=5 (it should be 20). With 'W' it stop at 6 instead of 26 and 'Return' stop at 10 instead of 40. I have yet to see a key that stop at the correct value. I get the same thing with .NET/mono and windows/mac. Anyone successfully used this function using the csharp bindings?

Was it helpful?

Solution

SDL_GetKeyboardState returns an array of Uint8, not uint! As you walk through the array, each step you are seeing sizeof(uint) bytes and checking if an unsigned int value is non 0. At i=5, you're checking 4 scancodes, one of them being SDLK_SCANCODE_Q.

Check the docs: https://wiki.libsdl.org/SDL_GetKeyboardState.

I hope your compiler warned you about this type mismatch.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top