-
Notifications
You must be signed in to change notification settings - Fork 2k
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
SDL3 key questions #10139
Comments
@libsdl-org/a-team, sanity check? |
Prior art:
Pros for unmodified keys:
Cons for unmodified keys:
Pros for modified keys:
Thoughts:
It seems like for key bindings, you're either binding positionally, in which case you'd be looking at scancodes and not keycodes, or you'd want to show the final character code that is used for the binding regardless of the key sequence that got you there. For example, you could map an action to '>' and it would either be Shift+'.' on a US layout, or the '>' key on an international keyboard. An even better example is mapping an action to 'ù', which is a key on a French keyboard, but would have to be entered via ALT+0249 on a US keyboard, and would never show up as a distinct key event. Anecdotally, web events give you the key 'ъ' with the code BracketRight, when you press that key with the Russian keyboard layout. |
An alternative might be to leave keycode as it was in SDL2, and add One downside to this would be that application developers would be confused as to which they should be using, especially since I'm thinking the additional confusion isn't worth it here, and we should just decide what keycode means and allow applications to switch that meaning or use SDL_GetKeyFromScancode() depending on their needs. |
After discussion with @icculus, we agreed the right thing is to remove the keycode entirely from the keyboard event. That will make it so most programs that just want to use the keyboard as a 101 key gamepad will do the right thing, and programs that want keycodes will have precise control over exactly how they want them. For desktop platforms we'll have the keymap available, and for web platforms we'll dynamically build the keymap as keyboard events come in. |
With this change, the answers to the original questions are easy:
The application can look up keycodes using modifiers or not, as they wish.
The names of the keycodes are the printable character for printable keycodes, or the name of the associated scancode for unprintable keycodes.
The scancode will remain SDL_SCANCODE_KP_4, for example, but the keycode will be SDLK_LEFT or SDLK_4, depending on numlock state. |
So this ends up not working well because we want scancodes to represent the physical key locations, but people may have remapped keys so semantically they've swapped modifier keys or moved escape closer to the center of the keyboard. The right thing for applications is to look at the keycode as the semantic meaning of the key, as they have been doing all along. I think that leaves us with making the keycode the base key as it is in SDL2, giving us the following answers:
Events will have unmodified keys. Also the hint will allow using French number keycodes and forcing English keycodes for non-latin keyboards as user options, but we'll remove the option for modified keys, because that's something applications would have to code for to handle correctly. We'll still have a function to take a scancode and modifier and return the actual key in the keymap regardless of the keycode in the event, so
Key names are not related to key layout, and the name for both 'a' and 'A' keycodes is "A". We'll document this. Separately we'll rename SDLK_[a-z] to SDLK_[A-Z], but leave the values lower case.
The keycode in the event and the layout will be SDLK_LEFT or SDLK_4 when the scancode is SDL_SCANCODE_KP_4, depending on the numlock modifier state. |
I've started writing up a "best practices" wiki page for this, with the final details and code examples to be filled in when code changes are pushed for this issue. I've barely proofread this, so feel free to make edits directly if you like: |
Mirroring the joystick and gamepad APIs, would it make sense to generate separate keyboard events, so users can simply consider the ones they care about and maybe disable the others? |
I added some code for the chat box and text editor use cases. |
Q1: Should event keycodes be modified keys or unmodified keys by default?
Q2: Should key names be the label printed on the key as in SDL2, or be the value of keycode? e.g. should SDLK_a and SDLK_A have the same name? Should SDL_SCANCODE_1 be '1' or '&' on the French keyboard? What should the name of SDLK_AMPERSAND be on the French keyboard?
Q3: Should we have separate SDL keycodes for numpad keys based on numlock state? As prior art, Windows has entirely different VK codes for those keys depending on whether numlock is on, and X11 has separate KP_Left and KP_4 keysyms.
Related issues:
#3559
#10150
The text was updated successfully, but these errors were encountered: