[SDL2] Keyboard State doesn't work for Backspace/F-Keys anymore?

Hi.

Apparently, some buttons don’t work anymore when used with GetKeyboardState - I had the same code working not too long ago. I’m on Arch Linux, using sdl2_compat - is this a bug of the distribution?

const Uint8 *state = SDL_GetKeyboardState(NULL);

if (state[SDL_GetScancodeFromKey(1073741906)] && (KeyCooldowns[KeyN]<=0)) 
			{
			  
			    KeyCooldowns[KeyN]=KeyCooldown;
				std::cout << "This will work" << std::endl;
			  
			}
if (state[SDL_GetScancodeFromKey(8)] && (KeyCooldowns[KeyN]<=0)) 
			{
			  
			    KeyCooldowns[KeyN]=KeyCooldown;
				std::cout << "This will not work" << std::endl;
			  
			}

if (state[SDL_GetScancodeFromKey(1073741882)] && (KeyCooldowns[KeyN]<=0)) 
			{
			  
			    KeyCooldowns[KeyN]=KeyCooldown;
				std::cout << "This neither?" << std::endl;
			  
			}

Greetings

Thunder

why are you doing SDL_GetScancodeFromKey ?

why not just pass key name ?

https://wiki.libsdl.org/SDL3/SDL_Scancode

like these

    SDL_SCANCODE_RETURN,
    SDL_SCANCODE_ESCAPE,
    SDL_SCANCODE_BACKSPACE,
    SDL_SCANCODE_TAB,
    SDL_SCANCODE_SPACE,

from Lazy Foo' Productions - Key States

//Set texture based on current keystate
                const Uint8* currentKeyStates = SDL_GetKeyboardState( NULL );
                if( currentKeyStates[ SDL_SCANCODE_UP ] )
                {
                    currentTexture = &gUpTexture;
                }
                else if( currentKeyStates[ SDL_SCANCODE_DOWN ] )
                {
                    currentTexture = &gDownTexture;
                }
                else if( currentKeyStates[ SDL_SCANCODE_LEFT ] )
                {
                    currentTexture = &gLeftTexture;
                }
                else if( currentKeyStates[ SDL_SCANCODE_RIGHT ] )
                {
                    currentTexture = &gRightTexture;
                }
                else
                {
                    currentTexture = &gPressTexture;
                }

Scancodes represent positions on the keyboard so if you care about the actual key, not the position, then it makes sense to use SDL_Keycode and SDL_GetScancodeFromKey. For example, SDL_SCANCODE_A correspond to the Q key on an AZERTY keyboard so using SDL_SCANCODE_A when you want the A key (regardless of where it is located) is wrong.

Using named constants is a good idea, but use the keycode constants in that case.

E.g. state[SDL_GetScancodeFromKey(SDLK_UP)]
instead of state[SDL_GetScancodeFromKey(1073741906)).

Scancodes represent positions on the keyboard so if you care about the actual key, not the position, then it makes sense to use SDL_Keycode

yes, obviously

but I never said anything about the keycode

I asked why use this ?

and SDL_GetScancodeFromKey

I literally posted a link to the scancodes so there is no need to use SDL_GetScancodeFromKey as far as I can tell

and the function takes scan codes

https://wiki.libsdl.org/SDL3/SDL_GetKeyboardState

The pointer returned is a pointer to an internal SDL array. It will be valid for the whole lifetime of the application and should not be freed by the caller.

A array element with a value of true means that the key is pressed and a value of false means that it is not. Indexes into this array are obtained by using SDL_Scancode values.

https://wiki.libsdl.org/SDL3/SDL_Scancode

Using scancodes directly is not the same. The scancodes returned from SDL_GetScancodeFromKey depend on your keyboard layout.

That is why SDL_GetScancodeFromKey has to be used if we want to use keycodes.


The page https://wiki.libsdl.org/SDL3/BestKeyboardPractices explains the distinction between scancodes and keycodes pretty well.

The 101-Button Joystick

Many games just want to treat a keyboard not as a way to input text, but just as a joystick that has a lot of buttons. The well-known “WASD” key pattern for FPS games is a fine example of this: you want the physical location of a key, regardless of what symbol is printed on the key. After all, on a French keyboard, instead of “WASD”, you’d press “ZQSD”, and on a Hiragana keyboard “てちとし”. Same locations on the keyboard, totally different symbols.

For these, you want SDL_Scancodes: these are guaranteed to reference the physical location on the keyboard and not what is printed on it.

Specifically, they assume a US English QWERTY keyboard layout, no matter what the keyboard in use actually has at that location, but that’s okay because here we just want physical location of the key, not its meaning.

The Specific Key

Some games might want to know the symbol on a key. This tends to be a “press ‘I’ to open your inventory” thing, and you don’t really care where the ‘I’ key is on the keyboard.

This is also useful for looking for the ESC key to cancel an operation, or Enter to confirm, etc; it doesn’t matter where the key is, you still want that key.

These are SDL_Keycodes. They name specific keys and don’t care where on the user’s keyboard they actually are.

1 Like