cocoa: Set keyboard mod state correctly when turning off capslock.
authorRyan C. Gordon <icculus@icculus.org>
Thu, 11 Jul 2019 01:07:14 -0400
changeset 1293741528cbd80ab
parent 12936 fe7caa031d3e
child 12938 165d577cdc63
cocoa: Set keyboard mod state correctly when turning off capslock.

Fixes Bugzilla #4716.
src/video/cocoa/SDL_cocoawindow.m
     1.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Wed Jul 10 10:06:28 2019 +0200
     1.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Thu Jul 11 01:07:14 2019 -0400
     1.3 @@ -866,7 +866,17 @@
     1.4         10.15 beta, capslock comes through here as keycode 255, but it's safe
     1.5         to send duplicate key events; SDL filters them out quickly in
     1.6         SDL_SendKeyboardKey(). */
     1.7 -    SDL_SendKeyboardKey(([theEvent modifierFlags] & NSEventModifierFlagCapsLock) ? SDL_PRESSED : SDL_RELEASED, SDL_SCANCODE_CAPSLOCK);
     1.8 +
     1.9 +    /* Also note that SDL_SendKeyboardKey expects all capslock events to be
    1.10 +       keypresses; it won't toggle the mod state if you send a keyrelease.  */
    1.11 +    const SDL_bool osenabled = ([theEvent modifierFlags] & NSEventModifierFlagCapsLock) ? SDL_TRUE : SDL_FALSE;
    1.12 +    const SDL_bool sdlenabled = (SDL_GetModState() & KMOD_CAPS) ? SDL_TRUE : SDL_FALSE;
    1.13 +    if (!osenabled && sdlenabled) {
    1.14 +        SDL_ToggleModState(KMOD_CAPS, SDL_FALSE);
    1.15 +        SDL_SendKeyboardKey(SDL_RELEASED, SDL_SCANCODE_CAPSLOCK);
    1.16 +    } else if (osenabled && !sdlenabled) {
    1.17 +        SDL_SendKeyboardKey(SDL_PRESSED, SDL_SCANCODE_CAPSLOCK);
    1.18 +    }
    1.19  }
    1.20  - (void)keyDown:(NSEvent *)theEvent
    1.21  {