Fix keyboard event bug where mouse grab combination won't work if SDL_GetModState #98
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.
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:
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 of0x1000 | 0x40
or0x2000 | 0x40
if caps lock is enabled, meaningSDL_GetModState() == KMOD_LCTRL
never gets evaluated to true if CTRL+G is pressed as it erroneously gets evaluated to0x1040 == 0x40
if the combination is pressed if numlock is enabled, or0x2040 == 0x40
if capslock is enabled.The solution involves getting the mask of
SDL_GetModState()
andKMOD_LCTRL
so that the expressionSDL_GetModState() == KMOD_LCTRL
can be checked properly regardless if the keyboard has any combination of caps lock/numlock enabled.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