Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDL2 SDL_Keycode returns characters in current keyboard layout #10002

Closed
eszdman opened this issue Jun 10, 2024 · 2 comments
Closed

SDL2 SDL_Keycode returns characters in current keyboard layout #10002

eszdman opened this issue Jun 10, 2024 · 2 comments

Comments

@eszdman
Copy link

eszdman commented Jun 10, 2024

System:

OS: Arch Linux x86_64
Kernel: 6.9.3-arch1-1 
DE: Plasma 6.0.5 
Graphics Platform: Wayland

Same issue using X11.

When I get SDL_Keycode from key press event I get symbols in keyboard layout at startup, this means that this doesn't correspond to a SDL_Keycode value in the enumeration.

Examples for qwertyuiop keys range:

Startup with en-US selected layout works as intended

keycode: 113
keycode: 113
keycode: 119
keycode: 119
keycode: 101
keycode: 101
keycode: 114
keycode: 114
keycode: 116
keycode: 116
keycode: 121
keycode: 121
keycode: 117
keycode: 117
keycode: 105
keycode: 105
keycode: 111
keycode: 111
keycode: 112
keycode: 112

Startup with ru-RU selected layout causes incorrect recognition of pressed buttons

keycode: 1081
keycode: 1081
keycode: 1094
keycode: 1094
keycode: 1091
keycode: 1091
keycode: 1082
keycode: 1082
keycode: 1077
keycode: 1077
keycode: 1085
keycode: 1085
keycode: 1075
keycode: 1075
keycode: 1096
keycode: 1096
keycode: 1097
keycode: 1097
keycode: 1079
keycode: 1079

Is it supposed to work like this or is it a bug since I can't recognize which buttons were pressed using SDL_Keycode enum?

@Kaktus514
Copy link
Contributor

Kaktus514 commented Jun 10, 2024

That is the expected behaviour.

From https://wiki.libsdl.org/SDL2/SDLKeycodeLookup:

Keycodes for keys with printable characters are represented by their Unicode code points.

The Unicode for Q is 113.
The Unicode for й is 1081.

A Russian keyboard has the letter й at the same location as an English keyboard has the letter Q so if you use an English keyboard and change the keyboard layout to Russian then you will get the keycode 1081 when pressing Q.

If you want to handle keys independent of the keyboard layout you should use scancodes instead of keycodes.

@slouken
Copy link
Collaborator

slouken commented Jun 10, 2024

If you want to know what the key would be on an en-US keyboard, you can use the function SDL_GetDefaultKeyFromScancode() which was introduced in SDL3 for exactly this purpose.

FYI, SDL3 also removes the SDL_Keycode enum and turns the type into Uint32, so it's more clear that it's not a predefined set of values, the keycode can be any printable key on the keyboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants