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

[rcore] Fix GetKeyPressed for PLATFORM_DESKTOP_SDL #3869

Merged
merged 1 commit into from
Apr 15, 2024

Conversation

The-Night-Watch
Copy link
Contributor

The key handling in PLATFORM_DESKTOP_SDL was faulty in two ways, which led to GetKeyPressed returning incorrect data.

CORE.Input.Keyboard.keyPressedQueue was updated only on SDL_TEXTINPUT, meaning only text characters were registered as a pressed key, but not function keys (eg. tab, backspace...).

Also on such event, both CORE.Input.Keyboard.keyPressedQueue and CORE.Input.Keyboard.charPressedQueue were assigned the key's corresponding codepoint, when CORE.Input.Keyboard.keyPressedQueue should get the raylib keycode instead.

CORE.Input.Keyboard.keyPressedQueue is now updated on SDL_KEYDOWN event instead.

The key handling in PLATFORM_DESKTOP_SDL was faulty in two ways, which
led to GetKeyPressed returning incorrect data.

CORE.Input.Keyboard.keyPressedQueue was updated only on SDL_TEXTINPUT,
meaning only text characters were registered as a pressed key, but not
function keys (eg. tab, backspace...). Also on such event, both
CORE.Input.Keyboard.keyPressedQueue and
CORE.Input.Keyboard.charPressedQueue were assigned the key's
corresponding codepoint, when CORE.Input.Keyboard.keyPressedQueue
should get the raylib keycode instead.

CORE.Input.Keyboard.keyPressedQueue is now updated on SDL_KEYDOWN event
instead.
@raysan5 raysan5 changed the title Fix GetKeyPressed for PLATFORM_DESKTOP_SDL [rcore] Fix GetKeyPressed for PLATFORM_DESKTOP_SDL Mar 31, 2024
@RobLoach
Copy link
Contributor

RobLoach commented Apr 4, 2024

How would one test this? Is there an example I couldt ry it out on?

@raysan5
Copy link
Owner

raysan5 commented Apr 5, 2024

@RobLoach You need to build raylib for PLATFORM_DESKTOP_SDL but not sure if there is any example to test it properly.

@The-Night-Watch Is this change consistent with GLFW?

@The-Night-Watch
Copy link
Contributor Author

@raysan5 Yes the idea was to align to PLATFORM_DESKTOP behavior.

@RobLoach Simplest example would be to compare the following on PLATFORM_DESKTOP and PLATFORM_DESKTOP_SDL before and after the patch:

while (!WindowShouldClose()) {
    int key = GetKeyPressed();
    int character = GetCharPressed();

    if (key) TraceLog(LOG_INFO, "key: %i", key);
    if (character) TraceLog(LOG_INFO, "char: %i", character);
    if (IsKeyPressed(KEY_BACKSPACE)) TraceLog(LOG_INFO, "backspace pressed");
}

Try typing function keys to see the issue.

@RobLoach
Copy link
Contributor

Confirming that with this PR in both SDL and glfw, pressing X returns....

INFO: key: 88
INFO: char: 120

Before this PR, SDL would return...

INFO: key: 120
INFO: char: 120

Thanks!

@The-Night-Watch
Copy link
Contributor Author

The-Night-Watch commented Apr 15, 2024

Also, before patch, pressing non text keys (eg. backspace) would lead to a false negative on IsKeyPressed(KEY_BACKSPACE) in PLATFORM_DESKTOP_SDL.

Nevermind, IsKeyPressed was actually correct.

@raysan5 raysan5 merged commit e42f926 into raysan5:master Apr 15, 2024
14 checks passed
@raysan5
Copy link
Owner

raysan5 commented Apr 15, 2024

@The-Night-Watch thanks for the improvement!

@RobLoach Thanks for reviewing it!

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

Successfully merging this pull request may close these issues.

3 participants