We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 3125

Summary: SDL_GetModState does not track Caps lock properly
Product: SDL Reporter: Kutta Srinivasan <kutta.s>
Component: eventsAssignee: Sam Lantinga <slouken>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: icculus, iryont, philipp.wiesemann
Version: 2.0.3   
Hardware: x86_64   
OS: Windows 7   

Description Kutta Srinivasan 2015-09-17 17:55:41 UTC
When launching our SDL app, if the user has CAPS LOCK on (i.e. if the OS and their hardware keyboard think that caps lock is on and generate capital letters as text input), SDL_GetModState will return 0. Toggling CAPS LOCK off will then cause SDL_GetModState to return 8192 (0x2000 - KMOD_CAPS). The same behavior can be observed if you alt-tab out, change the caps lock state, then alt tab back in - SDL_GetModState will return whatever value it was returning before even though you have changed the caps lock state.

If this is not a simple repro for you, I can dig deeper to try and figure out why this is the case. I am assuming it's something as simple as SDL assuming that all key mods are off at the beginning of the app and not actually querying the OS to see if caps lock is currently on, thereby putting SDL out of sync with the OS>

What's interesting about this bug is that SDL_TEXTINPUT will always correctly produce the right case of letter based on the OS/hardware caps lock state. Presumably that is going through some other mechanism which is aware of the correct setting of caps lock and not actually using SDL_GetModState to uppercase letters.
Comment 1 Philipp Wiesemann 2015-09-17 18:25:05 UTC
This is maybe related to bug 2736.
Comment 2 Kutta Srinivasan 2015-09-17 19:49:19 UTC
Yep, sounds like 2736 is the same bug - SDL probably doesn't ask (on any platform) for the current toggle state of the num / caps lock keys on startup or after regaining focus, so it's easy for it to get out of sync.
Comment 3 Konrad 2015-10-31 23:36:02 UTC
Hello

I believe it might be worth to bring this bug up. It's quite frustrating.
Comment 4 Ryan C. Gordon 2015-12-28 00:03:51 UTC
Capslock (numlock, scrolllock) having wrong values at startup is now fixed on Windows by https://hg.libsdl.org/SDL/rev/be3d04927338 ...

https://hg.libsdl.org/SDL/rev/75871b5e0bc6 fixes this on Mac OS X.

Our X11 code seems to handle this correctly, but I don't if that was true when this bug report was opened.

I haven't fixed this state getting out of sync on focus change on Windows (it's possible the Mac-specific code already handles this, though).

--ryan.
Comment 5 Ryan C. Gordon 2015-12-28 00:05:22 UTC
(In reply to Ryan C. Gordon from comment #4)
> Our X11 code seems to handle this correctly, but I don't if that was true
> when this bug report was opened.

This was fixed for X11 in https://hg.libsdl.org/SDL/rev/7099e2df7b29, which is what Bug #2736 was dealing with.

--ryan.
Comment 6 Ryan C. Gordon 2015-12-28 18:52:26 UTC
(In reply to Ryan C. Gordon from comment #4)
> Capslock (numlock, scrolllock) having wrong values at startup is now fixed
> on Windows by https://hg.libsdl.org/SDL/rev/be3d04927338 ...
> 
> https://hg.libsdl.org/SDL/rev/75871b5e0bc6 fixes this on Mac OS X.
> 
> Our X11 code seems to handle this correctly, but I don't if that was true
> when this bug report was opened.
> 
> I haven't fixed this state getting out of sync on focus change on Windows
> (it's possible the Mac-specific code already handles this, though).

Okay, as of the latest in revision control:

- You _don't_ get a key event if caps/numlock is toggled on at startup, but SDL_GetModState(), etc, will properly report them as toggled.

- You _don't_ get a key event if caps/numlock is toggled while your app is not in the foreground, but SDL will update its internal state when it regains focus, and then SDL_GetModState(), etc, will properly report them as toggled.

- You _will_ get a key event if these keys are toggled on or off by the user pressing them while your SDL app has focus. You'll get a KEYDOWN followed immediately by a KEYUP, regardless of whether the key is toggling on or off. This is how SDL2 has always worked, so I kept this behavior.

This has been fixed and tested on Windows, Mac OS X, and X11, using the checkkeys program in the test directory. Relevant changesets are:

    https://hg.libsdl.org/SDL/rev/75871b5e0bc6
    https://hg.libsdl.org/SDL/rev/be3d04927338
    https://hg.libsdl.org/SDL/rev/734c90ea9990
    https://hg.libsdl.org/SDL/rev/4d8a561cf978
    https://hg.libsdl.org/SDL/rev/05770dc93c95
    https://hg.libsdl.org/SDL/rev/bb853b9549d0

I'm marking this resolved. Thanks!

--ryan.