Skip to content

Commit

Permalink
[HTML5] Add support for physical_keycode.
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
Faless authored and bruvzg committed May 6, 2021
1 parent dab4cf3 commit 8740e95
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 21 deletions.
40 changes: 21 additions & 19 deletions platform/javascript/dom_keys.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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; \
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions platform/javascript/os_javascript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down

0 comments on commit 8740e95

Please sign in to comment.