Skip to content

Commit 4a4e165

Browse files
authored
Ignore unknown keyCodes in library_sdl.js (#22920)
Prior to this change any unrecognized DOM keyCode would be directly passed through with the DOM keyCode used as an SDL keycode. This strange behavior is what allowed `test_sdl_key_proxy` to use SDL key codes when generating new `KeyboardEvent`s. The number previously hardcoded in the test (1248, 1249, 1250) correspond to (SDLK_LCTRL, SDLK_LSHIFT, SDLK_LALT) but don't make any sense as DOM keyboard codes.
1 parent a90f70f commit 4a4e165

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

src/library_sdl.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -823,13 +823,16 @@ var LibrarySDL = {
823823

824824
lookupKeyCodeForEvent(event) {
825825
var code = event.keyCode;
826-
if (code >= 65 && code <= 90) {
826+
if (code >= 65 && code <= 90) { // ASCII A-Z
827827
code += 32; // make lowercase for SDL
828828
} else {
829+
// Look up DOM code in the keyCodes table with fallback for ASCII codes
830+
// which can match between DOM codes and SDL keycodes (allows keyCodes
831+
// to be smaller).
832+
code = SDL.keyCodes[code] || (code < 128 ? code : 0);
829833
#if RUNTIME_DEBUG
830-
if (!(event.keyCode in SDL.keyCodes)) dbg('unknown keyCode: ', event.keyCode);
834+
if (!code) dbg('unmapped keyCode: ', event.keyCode);
831835
#endif
832-
code = SDL.keyCodes[event.keyCode] || event.keyCode;
833836
// If this is one of the modifier keys (224 | 1<<10 - 227 | 1<<10), and the event specifies that it is
834837
// a right key, add 4 to get the right key SDL key code.
835838
if (event.location === 2 /*KeyboardEvent.DOM_KEY_LOCATION_RIGHT*/ && code >= (224 | 1<<10) && code <= (227 | 1<<10)) {
@@ -854,6 +857,8 @@ var LibrarySDL = {
854857
case 'keyup': {
855858
var down = event.type === 'keydown';
856859
var code = SDL.lookupKeyCodeForEvent(event);
860+
// Ignore key events that we don't (yet) map to SDL keys
861+
if (!code) return;
857862
#if !SAFE_HEAP
858863
// Assigning a boolean to HEAP8, that's alright but Closure would like to warn about it.
859864
// TODO(https://github.com/emscripten-core/emscripten/issues/16311):
@@ -938,6 +943,8 @@ var LibrarySDL = {
938943
dbg(`received ${event.type} event: keyCode=${event.keyCode}, key=${event.key}, code=${event.code}`);
939944
#endif
940945
var key = SDL.lookupKeyCodeForEvent(event);
946+
// Ignore key events that we don't (yet) map to SDL keys
947+
if (!key) return false;
941948
var scan;
942949
if (key >= 1024) {
943950
scan = key - 1024;

test/browser/test_sdl_key.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ int main(int argc, char **argv) {
7272

7373
emscripten_set_main_loop(one, 0, 0);
7474

75-
EM_ASM({keydown(1250);keydown(38);keyup(38);keyup(1250);}); // alt, up
76-
EM_ASM({keydown(1248);keydown(1249);keydown(40);keyup(40);keyup(1249);keyup(1248);}); // ctrl, shift, down
75+
EM_ASM({keydown(18);keydown(38);keyup(38);keyup(18);}); // alt, up
76+
EM_ASM({keydown(17);keydown(16);keydown(40);keyup(40);keyup(16);keyup(17);}); // ctrl, shift, down
7777
EM_ASM({keydown(37);keyup(37);}); // left
7878
EM_ASM({keydown(39);keyup(39);}); // right
7979

test/test_browser.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -990,8 +990,8 @@ def post():
990990
html = html.replace('</body>', '''
991991
<script src='fake_events.js'></script>
992992
<script>
993-
simulateKeyDown(1250);simulateKeyDown(38);simulateKeyUp(38);simulateKeyUp(1250); // alt, up
994-
simulateKeyDown(1248);simulateKeyDown(1249);simulateKeyDown(40);simulateKeyUp(40);simulateKeyUp(1249);simulateKeyUp(1248); // ctrl, shift, down
993+
simulateKeyDown(18);simulateKeyDown(38);simulateKeyUp(38);simulateKeyUp(18); // alt, up
994+
simulateKeyDown(17);simulateKeyDown(16);simulateKeyDown(40);simulateKeyUp(40);simulateKeyUp(16);simulateKeyUp(17); // ctrl, shift, down
995995
simulateKeyDown(37);simulateKeyUp(37); // left
996996
simulateKeyDown(39);simulateKeyUp(39); // right
997997
simulateKeyDown(65);simulateKeyUp(65); // a

0 commit comments

Comments
 (0)