From 8740e95f15119661fb129bc27045f271b128d701 Mon Sep 17 00:00:00 2001 From: Fabio Alessandrelli Date: Mon, 8 Mar 2021 10:16:22 +0100 Subject: [PATCH] [HTML5] Add support for `physical_keycode`. This uses the `event.code` value to retrieve the physical code, while still using the extra logic to map the unicode value to our keylist, when computing the `scancode` (supporting ASCII and Latin-1). --- platform/javascript/dom_keys.inc | 40 ++++++++++++++------------- platform/javascript/os_javascript.cpp | 4 +-- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/platform/javascript/dom_keys.inc b/platform/javascript/dom_keys.inc index 060952600a8e..7902efafe000 100644 --- a/platform/javascript/dom_keys.inc +++ b/platform/javascript/dom_keys.inc @@ -31,7 +31,7 @@ #include "core/os/keyboard.h" // See https://w3c.github.io/uievents-code/#code-value-tables -int dom_code2godot_scancode(EM_UTF8 const p_code[32], EM_UTF8 const p_key[32]) { +int 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; \ @@ -71,31 +71,33 @@ int dom_code2godot_scancode(EM_UTF8 const p_code[32], EM_UTF8 const p_key[32]) { DOM2GODOT("NumpadSubtract", KP_SUBTRACT); // Printable ASCII. - 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; + 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 b0; } - return 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; - } - if (key >= 0xE0 && key <= 0xFF) { // Lowercase known latin. - key -= 0x20; - return key; + // 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; + } + if (key >= 0xE0 && key <= 0xFF) { // Lowercase known latin. + key -= 0x20; + return key; + } } - } #undef _U_2BYTES_MASK #undef _U_2BYTES + } // Alphanumeric section. DOM2GODOT("Backquote", QUOTELEFT); diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp index 2657cb57fdd2..35454a546e4b 100644 --- a/platform/javascript/os_javascript.cpp +++ b/platform/javascript/os_javascript.cpp @@ -240,8 +240,8 @@ static Ref setup_key_event(const EmscriptenKeyboardEvent *emscrip ev.instance(); ev->set_echo(emscripten_event->repeat); dom2godot_mod(emscripten_event, ev); - ev->set_scancode(dom_code2godot_scancode(emscripten_event->code, emscripten_event->key)); - ev->set_physical_scancode(dom_code2godot_scancode(emscripten_event->code, emscripten_event->key)); + ev->set_scancode(dom_code2godot_scancode(emscripten_event->code, emscripten_event->key, false)); + ev->set_physical_scancode(dom_code2godot_scancode(emscripten_event->code, emscripten_event->key, true)); String unicode = String::utf8(emscripten_event->key); // Check if empty or multi-character (e.g. `CapsLock`).