diff options
| author | Andre Weissflog <floooh@gmail.com> | 2023-04-01 14:07:06 +0200 |
|---|---|---|
| committer | Andre Weissflog <floooh@gmail.com> | 2023-04-01 14:07:10 +0200 |
| commit | 17a56ff3dde0abb89734bc82af6004df4ff0cb61 (patch) | |
| tree | e0b5eecd07b8005ac50c6f7ea54a77a42afe2352 /sokol_app.h | |
| parent | 49ed5328da48415e8f63819eff3a1c970a4e8009 (diff) | |
sokol_app.h emsc: fallback to EmscriptenKeyboardEvent.key if .code isn't populated (fixes #809)
Diffstat (limited to 'sokol_app.h')
| -rw-r--r-- | sokol_app.h | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/sokol_app.h b/sokol_app.h index 152cce06..d18efe81 100644 --- a/sokol_app.h +++ b/sokol_app.h @@ -5361,6 +5361,7 @@ _SOKOL_PRIVATE EM_BOOL _sapp_emsc_key_cb(int emsc_type, const EmscriptenKeyboard _sapp.event.key_repeat = emsc_event->repeat; _sapp.event.modifiers = _sapp_emsc_key_event_mods(emsc_event); if (type == SAPP_EVENTTYPE_CHAR) { + // FIXME: this doesn't appear to work on Android Chrome _sapp.event.char_code = emsc_event->charCode; /* workaround to make Cmd+V work on Safari */ if ((emsc_event->metaKey) && (emsc_event->charCode == 118)) { @@ -5368,7 +5369,18 @@ _SOKOL_PRIVATE EM_BOOL _sapp_emsc_key_cb(int emsc_type, const EmscriptenKeyboard } } else { - _sapp.event.key_code = _sapp_emsc_translate_key(emsc_event->code); + if (0 != emsc_event->code[0]) { + // This code path is for desktop browsers which send untranslated 'physical' key code strings + // (which is what we actually want for key events) + _sapp.event.key_code = _sapp_emsc_translate_key(emsc_event->code); + } else { + // This code path is for mobile browsers which only send localized key code + // strings. Note that the translation will only work for a small subset + // of localization-agnostic keys (like Enter, arrow keys, etc...), but + // regular alpha-numeric keys will all result in an SAPP_KEYCODE_INVALID) + _sapp.event.key_code = _sapp_emsc_translate_key(emsc_event->key); + } + /* Special hack for macOS: if the Super key is pressed, macOS doesn't send keyUp events. As a workaround, to prevent keys from "sticking", we'll send a keyup event following a keydown @@ -5381,7 +5393,7 @@ _SOKOL_PRIVATE EM_BOOL _sapp_emsc_key_cb(int emsc_type, const EmscriptenKeyboard { send_keyup_followup = true; } - /* only forward a certain key ranges to the browser */ + // only forward keys to the browser (can further be supressed by sapp_consume_event()) switch (_sapp.event.key_code) { case SAPP_KEYCODE_WORLD_1: case SAPP_KEYCODE_WORLD_2: @@ -5447,7 +5459,7 @@ _SOKOL_PRIVATE EM_BOOL _sapp_emsc_key_cb(int emsc_type, const EmscriptenKeyboard } } if (_sapp_call_event(&_sapp.event)) { - /* consume event via sapp_consume_event() */ + // event was consumed event via sapp_consume_event() retval = true; } if (send_keyup_followup) { |