Explorar el Código

[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).
Fabio Alessandrelli hace 4 años
padre
commit
8740e95f15
Se han modificado 2 ficheros con 23 adiciones y 21 borrados
  1. 21 19
      platform/javascript/dom_keys.inc
  2. 2 2
      platform/javascript/os_javascript.cpp

+ 21 - 19
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);

+ 2 - 2
platform/javascript/os_javascript.cpp

@@ -240,8 +240,8 @@ static Ref<InputEventKey> 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`).