diff options
author | bruvzg <7645683+bruvzg@users.noreply.github.com> | 2022-12-11 01:21:22 +0200 |
---|---|---|
committer | bruvzg <7645683+bruvzg@users.noreply.github.com> | 2023-01-23 15:08:12 +0200 |
commit | daad4aed62bfa471421f960179f0ac0fd78e8040 (patch) | |
tree | 86b7f69d180f253c51c4b567139d68850f9e365d /platform/web | |
parent | 9937915ad77059b63582ffd4e324afb26f467b76 (diff) |
Cleanup and unify keyboard input.
- Unify keycode values (secondary label printed on a key), remove unused hardcoded Latin-1 codes.
- Unify IME behaviour, add inline composition string display on Windows and X11.
- Add key_label (localized label printed on a key) value to the key events, and allow mapping actions to the unshifted Unicode events.
- Add support for physical keyboard (Bluetooth or Sidecar) handling on iOS.
- Add support for media key handling on macOS.
Co-authored-by: Raul Santos <raulsntos@gmail.com>
Diffstat (limited to 'platform/web')
-rw-r--r-- | platform/web/display_server_web.cpp | 19 | ||||
-rw-r--r-- | platform/web/dom_keys.inc | 56 |
2 files changed, 34 insertions, 41 deletions
diff --git a/platform/web/display_server_web.cpp b/platform/web/display_server_web.cpp index fdb9d107a7..d71fd60543 100644 --- a/platform/web/display_server_web.cpp +++ b/platform/web/display_server_web.cpp @@ -121,18 +121,25 @@ void DisplayServerWeb::key_callback(int p_pressed, int p_repeat, int p_modifiers // Resume audio context after input in case autoplay was denied. OS_Web::get_singleton()->resume_audio(); + char32_t c = 0x00; + String unicode = String::utf8(key_event.key); + if (unicode.length() == 1) { + c = unicode[0]; + } + + Key keycode = dom_code2godot_scancode(key_event.code, key_event.key, false); + Key scancode = dom_code2godot_scancode(key_event.code, key_event.key, true); + Ref<InputEventKey> ev; ev.instantiate(); ev->set_echo(p_repeat); - ev->set_keycode(dom_code2godot_scancode(key_event.code, key_event.key, false)); - ev->set_physical_keycode(dom_code2godot_scancode(key_event.code, key_event.key, true)); + ev->set_keycode(fix_keycode(c, keycode)); + ev->set_physical_keycode(scancode); + ev->set_key_label(fix_key_label(c, keycode)); + ev->set_unicode(fix_unicode(c)); ev->set_pressed(p_pressed); dom2godot_mod(ev, p_modifiers); - String unicode = String::utf8(key_event.key); - if (unicode.length() == 1) { - ev->set_unicode(unicode[0]); - } Input::get_singleton()->parse_input_event(ev); // Make sure to flush all events so we can call restricted APIs inside the event. diff --git a/platform/web/dom_keys.inc b/platform/web/dom_keys.inc index 5f8d921bfb..e63bd7c69f 100644 --- a/platform/web/dom_keys.inc +++ b/platform/web/dom_keys.inc @@ -32,9 +32,9 @@ // See https://w3c.github.io/uievents-code/#code-value-tables Key dom_code2godot_scancode(EM_UTF8 const p_code[32], EM_UTF8 const p_key[32], bool p_physical) { -#define DOM2GODOT(p_str, p_godot_code) \ - if (memcmp((const void *)p_str, (void *)p_code, strlen(p_str) + 1) == 0) { \ - return Key::p_godot_code; \ +#define DOM2GODOT(p_str, p_godot_code) \ + if (memcmp((const void *)p_str, (void *)(p_physical ? p_key : p_code), strlen(p_str) + 1) == 0) { \ + return Key::p_godot_code; \ } // Numpad section. @@ -70,35 +70,6 @@ Key dom_code2godot_scancode(EM_UTF8 const p_code[32], EM_UTF8 const p_key[32], b DOM2GODOT("NumpadStar", KP_MULTIPLY); // or ASTERISK ? DOM2GODOT("NumpadSubtract", KP_SUBTRACT); - // Printable ASCII. - if (!p_physical) { - uint8_t b0 = (uint8_t)p_key[0]; - uint8_t b1 = (uint8_t)p_key[1]; - uint8_t b2 = (uint8_t)p_key[2]; - if (b1 == 0 && b0 > 0x1F && b0 < 0x7F) { // ASCII. - if (b0 > 0x60 && b0 < 0x7B) { // Lowercase ASCII. - b0 -= 32; - } - return (Key)b0; - } - -#define _U_2BYTES_MASK 0xE0 -#define _U_2BYTES 0xC0 - // Latin-1 codes. - if (b2 == 0 && (b0 & _U_2BYTES_MASK) == _U_2BYTES) { // 2-bytes utf8, only known latin. - uint32_t key = ((b0 & ~_U_2BYTES_MASK) << 6) | (b1 & 0x3F); - if (key >= 0xA0 && key <= 0xDF) { - return (Key)key; - } - if (key >= 0xE0 && key <= 0xFF) { // Lowercase known latin. - key -= 0x20; - return (Key)key; - } - } -#undef _U_2BYTES_MASK -#undef _U_2BYTES - } - // Alphanumeric section. DOM2GODOT("Backquote", QUOTELEFT); DOM2GODOT("Backslash", BACKSLASH); @@ -162,8 +133,8 @@ Key dom_code2godot_scancode(EM_UTF8 const p_code[32], EM_UTF8 const p_key[32], b DOM2GODOT("ControlLeft", CTRL); DOM2GODOT("ControlRight", CTRL); DOM2GODOT("Enter", ENTER); - DOM2GODOT("MetaLeft", SUPER_L); - DOM2GODOT("MetaRight", SUPER_R); + DOM2GODOT("MetaLeft", META); + DOM2GODOT("MetaRight", META); DOM2GODOT("ShiftLeft", SHIFT); DOM2GODOT("ShiftRight", SHIFT); DOM2GODOT("Space", SPACE); @@ -227,6 +198,21 @@ Key dom_code2godot_scancode(EM_UTF8 const p_code[32], EM_UTF8 const p_key[32], b DOM2GODOT("AudioVolumeMute", VOLUMEMUTE); DOM2GODOT("AudioVolumeUp", VOLUMEUP); //DOM2GODOT("WakeUp", UNKNOWN); - return Key::UNKNOWN; + + // Printable ASCII. + uint8_t b0 = (uint8_t)p_key[0]; + uint8_t b1 = (uint8_t)p_key[1]; + if (b0 >= 0x20 && b0 < 0x7F) { // ASCII. + if (b0 > 0x60 && b0 < 0x7B) { // Lowercase ASCII. + b0 -= 32; + } + return (Key)b0; + } else if (b0 == 0xC2 && b1 == 0xA5) { + return Key::YEN; + } else if (b0 == 0xC2 && b1 == 0xA7) { + return Key::SECTION; + } + + return Key::NONE; #undef DOM2GODOT } |