diff options
| author | Andre Weissflog <floooh@gmail.com> | 2019-11-09 15:35:15 +0100 |
|---|---|---|
| committer | Andre Weissflog <floooh@gmail.com> | 2019-11-09 15:35:15 +0100 |
| commit | 55826d7a686e79eaf14c9ed1d99cb595617976c0 (patch) | |
| tree | ea80815664e8fd61344c374432e8750719e04c55 /sokol_app.h | |
| parent | 939c071043707c283b3122a36cc3df9b533711cd (diff) | |
sokol_app.h: workaround for keys getting stuck when Cmd key is pressed
On macOS, both native and in browsers, keyUp events aren't
delivered by the OS when the Cmd key is pressed, which causes
key events to be stuck in sokol_app.h. The (hacky) workaround is
to not forward keyDown events to the app either while
Cmd is pressed.
Diffstat (limited to 'sokol_app.h')
| -rw-r--r-- | sokol_app.h | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/sokol_app.h b/sokol_app.h index bb2fb771..b904b015 100644 --- a/sokol_app.h +++ b/sokol_app.h @@ -1588,7 +1588,13 @@ _SOKOL_PRIVATE void _sapp_macos_app_event(sapp_event_type type) { - (void)keyDown:(NSEvent*)event { if (_sapp_events_enabled()) { const uint32_t mods = _sapp_macos_mod(event.modifierFlags); - _sapp_macos_key_event(SAPP_EVENTTYPE_KEY_DOWN, _sapp_translate_key(event.keyCode), event.isARepeat, mods); + /* NOTE: macOS doesn't send keyUp events while the Cmd key is pressed, + as a workaround, to prevent key presses from sticking we'll also not send + key down events. + */ + if (0 == (mods & SAPP_MODIFIER_SUPER)) { + _sapp_macos_key_event(SAPP_EVENTTYPE_KEY_DOWN, _sapp_translate_key(event.keyCode), event.isARepeat, mods); + } const NSString* chars = event.characters; const NSUInteger len = chars.length; if (len > 0) { @@ -2293,6 +2299,7 @@ _SOKOL_PRIVATE EM_BOOL _sapp_emsc_key_cb(int emsc_type, const EmscriptenKeyboard break; } if (type != SAPP_EVENTTYPE_INVALID) { + bool ignore_event = false; _sapp_init_event(type); _sapp.event.key_repeat = emsc_event->repeat; if (emsc_event->ctrlKey) { @@ -2312,6 +2319,16 @@ _SOKOL_PRIVATE EM_BOOL _sapp_emsc_key_cb(int emsc_type, const EmscriptenKeyboard } else { _sapp.event.key_code = _sapp_translate_key(emsc_event->keyCode); + /* Special hack for macOS: if the Super key is pressed, macOS doesn't + send keyUp events. As a workaround, to prevent keys from + "sticking", don't send keyDown events either while Super is + pressed. + */ + if ((_sapp.event.key_code != SAPP_KEYCODE_LEFT_SUPER) && + (_sapp.event.key_code != SAPP_KEYCODE_RIGHT_SUPER)) + { + ignore_event = 0 != (_sapp.event.modifiers & SAPP_MODIFIER_SUPER); + } /* only forward a certain key ranges to the browser */ switch (_sapp.event.key_code) { case SAPP_KEYCODE_WORLD_1: @@ -2377,7 +2394,9 @@ _SOKOL_PRIVATE EM_BOOL _sapp_emsc_key_cb(int emsc_type, const EmscriptenKeyboard break; } } - _sapp_call_event(&_sapp.event); + if (!ignore_event) { + _sapp_call_event(&_sapp.event); + } } } _sapp_emsc_update_keyboard_state(); |