From 768ef770a351ee1528e6e7923d3b3b00654401cb Mon Sep 17 00:00:00 2001 From: Ben Harris Date: Mon, 24 Oct 2022 23:06:12 +0100 Subject: [PATCH] js: Use KeyboardEvent.key for ASCII keystrokes This requires passing in KeyboardEvent.location from JavaScript so that we can detect the numeric keypad properly. Out of caution we currently only set MOD_NUM_KEYPAD on numbers, like we always have, but we have enough information to set it on arrow keys, Enter, "+", etc. This finally gets '/' and '\' working in Slant again. --- emcc.c | 14 +++++++++++++- emccpre.js | 6 +++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/emcc.c b/emcc.c index 33f7608..6c16248 100644 --- a/emcc.c +++ b/emcc.c @@ -262,9 +262,14 @@ void mousemove(int x, int y, int buttons) /* * Keyboard handler called from JS. */ -void key(int keycode, const char *key, const char *chr, +void key(int keycode, const char *key, const char *chr, int location, bool shift, bool ctrl) { + /* Key location constants from JavaScript. */ + #define DOM_KEY_LOCATION_STANDARD 0 + #define DOM_KEY_LOCATION_LEFT 1 + #define DOM_KEY_LOCATION_RIGHT 2 + #define DOM_KEY_LOCATION_NUMPAD 3 int keyevent = -1; if (!strnullcmp(key, "Backspace") || !strnullcmp(key, "Delete") || @@ -296,6 +301,9 @@ void key(int keycode, const char *key, const char *chr, keyevent = MOD_NUM_KEYPAD | '7'; else if (!strnullcmp(key, "PageUp")) keyevent = MOD_NUM_KEYPAD | '9'; + else if (key && (unsigned char)key[0] < 0x80 && key[1] == '\0') + /* Key generating a single ASCII character. */ + keyevent = key[0]; else if (keycode == 8 || keycode == 46) keyevent = 127; /* Backspace / Delete */ else if (keycode == 13) @@ -340,6 +348,10 @@ void key(int keycode, const char *key, const char *chr, keyevent &= 0x1F; } + if ('0' <= keyevent && keyevent <= '9' && + location == DOM_KEY_LOCATION_NUMPAD) + keyevent |= MOD_NUM_KEYPAD; + midend_process_key(me, 0, 0, keyevent); update_undo_redo(); } diff --git a/emccpre.js b/emccpre.js index 2a71875..450a093 100644 --- a/emccpre.js +++ b/emccpre.js @@ -311,10 +311,10 @@ function initPuzzle() { // the puzzle - so users of this puzzle collection in other media // can indulge their instinct to press ^R for redo, for example, // without accidentally reloading the page. - key = Module.cwrap('key', 'void', ['number', 'string', - 'string', 'number', 'number']); + key = Module.cwrap('key', 'void', ['number', 'string', 'string', + 'number', 'number', 'number']); onscreen_canvas.onkeydown = function(event) { - key(event.keyCode, event.key, event.char, + key(event.keyCode, event.key, event.char, event.location, event.shiftKey ? 1 : 0, event.ctrlKey ? 1 : 0); event.preventDefault(); };