Fixed incorrect modifier keys handling on macOS (cherry-picked from commit 45a58b7882a253db29b1b1393bc2a1fe030d5955)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
diff --git a/src/video/cocoa/SDL_cocoakeyboard.m b/src/video/cocoa/SDL_cocoakeyboard.m
index 78c8a16..f085ccc 100644
--- a/src/video/cocoa/SDL_cocoakeyboard.m
+++ b/src/video/cocoa/SDL_cocoakeyboard.m
@@ -182,47 +182,58 @@
@end
+static bool IsModifierKeyPressed(unsigned int flags,
+ unsigned int target_mask,
+ unsigned int other_mask,
+ unsigned int either_mask)
+{
+ bool target_pressed = (flags & target_mask) != 0;
+ bool other_pressed = (flags & other_mask) != 0;
+ bool either_pressed = (flags & either_mask) != 0;
+
+ if (either_pressed != (target_pressed || other_pressed))
+ return either_pressed;
+
+ return target_pressed;
+}
+
static void
-HandleModifiers(_THIS, unsigned short scancode, unsigned int modifierFlags)
+HandleModifiers(_THIS, SDL_Scancode code, unsigned int modifierFlags)
{
- SDL_Scancode code = darwin_scancode_table[scancode];
-
- const SDL_Scancode codes[] = {
- SDL_SCANCODE_LSHIFT,
- SDL_SCANCODE_LCTRL,
- SDL_SCANCODE_LALT,
- SDL_SCANCODE_LGUI,
- SDL_SCANCODE_RSHIFT,
- SDL_SCANCODE_RCTRL,
- SDL_SCANCODE_RALT,
- SDL_SCANCODE_RGUI,
- SDL_SCANCODE_LSHIFT,
- SDL_SCANCODE_LCTRL,
- SDL_SCANCODE_LALT,
- SDL_SCANCODE_LGUI, };
-
- const unsigned int modifiers[] = {
- NX_DEVICELSHIFTKEYMASK,
- NX_DEVICELCTLKEYMASK,
- NX_DEVICELALTKEYMASK,
- NX_DEVICELCMDKEYMASK,
- NX_DEVICERSHIFTKEYMASK,
- NX_DEVICERCTLKEYMASK,
- NX_DEVICERALTKEYMASK,
- NX_DEVICERCMDKEYMASK,
- NX_SHIFTMASK,
- NX_CONTROLMASK,
- NX_ALTERNATEMASK,
- NX_COMMANDMASK };
-
- for (int i = 0; i < 12; i++) {
- if (code == codes[i]) {
- if (modifierFlags & modifiers[i]) {
- SDL_SendKeyboardKey(SDL_PRESSED, code);
- } else {
- SDL_SendKeyboardKey(SDL_RELEASED, code);
- }
- }
+ bool pressed = false;
+
+ if (code == SDL_SCANCODE_LSHIFT) {
+ pressed = IsModifierKeyPressed(modifierFlags, NX_DEVICELSHIFTKEYMASK,
+ NX_DEVICERSHIFTKEYMASK, NX_SHIFTMASK);
+ } else if (code == SDL_SCANCODE_LCTRL) {
+ pressed = IsModifierKeyPressed(modifierFlags, NX_DEVICELCTLKEYMASK,
+ NX_DEVICERCTLKEYMASK, NX_CONTROLMASK);
+ } else if (code == SDL_SCANCODE_LALT) {
+ pressed = IsModifierKeyPressed(modifierFlags, NX_DEVICELALTKEYMASK,
+ NX_DEVICERALTKEYMASK, NX_ALTERNATEMASK);
+ } else if (code == SDL_SCANCODE_LGUI) {
+ pressed = IsModifierKeyPressed(modifierFlags, NX_DEVICELCMDKEYMASK,
+ NX_DEVICERCMDKEYMASK, NX_COMMANDMASK);
+ } else if (code == SDL_SCANCODE_RSHIFT) {
+ pressed = IsModifierKeyPressed(modifierFlags, NX_DEVICERSHIFTKEYMASK,
+ NX_DEVICELSHIFTKEYMASK, NX_SHIFTMASK);
+ } else if (code == SDL_SCANCODE_RCTRL) {
+ pressed = IsModifierKeyPressed(modifierFlags, NX_DEVICERCTLKEYMASK,
+ NX_DEVICELCTLKEYMASK, NX_CONTROLMASK);
+ } else if (code == SDL_SCANCODE_RALT) {
+ pressed = IsModifierKeyPressed(modifierFlags, NX_DEVICERALTKEYMASK,
+ NX_DEVICELALTKEYMASK, NX_ALTERNATEMASK);
+ } else if (code == SDL_SCANCODE_RGUI) {
+ pressed = IsModifierKeyPressed(modifierFlags, NX_DEVICERCMDKEYMASK,
+ NX_DEVICELCMDKEYMASK, NX_COMMANDMASK);
+ } else {
+ return;
+ }
+
+ if (pressed) {
+ SDL_SendKeyboardKey(SDL_PRESSED, code);
+ } else {
+ SDL_SendKeyboardKey(SDL_RELEASED, code);
}
}
@@ -423,7 +434,7 @@ Cocoa_HandleKeyEvent(_THIS, NSEvent *event)
SDL_SendKeyboardKey(SDL_RELEASED, code);
break;
case NSEventTypeFlagsChanged:
- HandleModifiers(_this, scancode, (unsigned int)[event modifierFlags]);
+ HandleModifiers(_this, code, (unsigned int)[event modifierFlags]);
break;
default: /* just to avoid compiler warnings */
break;