SDL3: Handle keyboard input asynchronously when text input is disabled #6506
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR updates keyboard events to be handled asynchronously in the SDL event filter iff text input is disabled.
For now, keyboard input is handled synchronously when text input is enabled to prevent timing problems. If keyboard events are handled asynchronously, a keyboard event might come before a text input event. This is opposite to the current order and may break
TextBox
consuming key events that produce text.TextBox
expects and is tested to first receive text, and then key input:osu-framework/osu.Framework.Tests/Visual/UserInterface/TestSceneTextBoxKeyEvents.cs
Lines 68 to 78 in 54a23fb
Threading concerns
There is a possibility for a key to get stuck in a pressed state if it's quickly tapped around the time text input is disabled.
Assume a key is quickly pressed and released, generating
SDL_EVENT_KEY_DOWN
and aSDL_EVENT_KEY_UP
.SDL_EVENT_KEY_DOWN
Ignored as text input is enabled
commandScheduler.Update()
SDL_StopTextInput()
SDL_EVENT_KEY_UP
Handled, added to input queue
SDL_EVENT_KEY_DOWN
Handled, added to input queue
Handle key up event (no state change)
Handle key down event
The key is now stuck. But the user can just press and release it to fix this.
The opposite is also possible: a key is pressed, but the game thinks it's released. Can happen if a held key is quickly released and pressed again.
This is only a concern if keyboard input is handled on a different thread, as things currently stand with SDL on Windows (SDL_HINT_WINDOWS_RAW_KEYBOARD is disabled by default), keyboard events are added to the SDL event queue and handled by our filter on the main thread.