Skip to content

Fix keyboard event bug where mouse grab combination won't work if SDL_GetModState #98

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

Merged
merged 2 commits into from
Jul 10, 2024

Conversation

dyharlan
Copy link
Contributor

I found this bug while trying to figure out why dingus won't work when compiled with MSVC (I still had to use Clang when compiling w/ vs2022).

Basically when the keyboard is in a specific state (NumLock, CapsLock, etc.) the code below:

if ( event.key.keysym.sym == SDLK_g && SDL_GetModState() == KMOD_LCTRL) {
    if (event.type == SDL_KEYUP) {
        toggle_mouse_grab(event.key);
    }
    return;
}

doesn't work because since if the keyboard is in a specific state, it returns the OR'd combination of said modifier, in this case CTRL, which has a value of 0x40, with the state of the keyboard.

So if the keyboard has all locks disabled, SDL_GetModState() returns the value of 0 | 0x40. But if NumLock is enabled, it returns the value of 0x1000 | 0x40 or 0x2000 | 0x40 if caps lock is enabled, meaning SDL_GetModState() == KMOD_LCTRL never gets evaluated to true if CTRL+G is pressed as it erroneously gets evaluated to 0x1040 == 0x40 if the combination is pressed if numlock is enabled, or 0x2040 == 0x40 if capslock is enabled.

The solution involves getting the mask of SDL_GetModState() and KMOD_LCTRL so that the expression SDL_GetModState() == KMOD_LCTRL can be checked properly regardless if the keyboard has any combination of caps lock/numlock enabled.

if (SDL_GetModState() & KMOD_LCTRL && event.key.keysym.sym == SDLK_g) {
    if (event.type == SDL_KEYUP) {
        toggle_mouse_grab(event.key);
    }
    return;
}

This was the reference I used to fix the issue:
https://stackoverflow.com/questions/48706762/sdl-2-how-to-check-for-no-modifiers-keyboard-input

@dingusdev dingusdev merged commit e2e78c0 into dingusdev:master Jul 10, 2024
5 checks passed
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.

2 participants