Skip to content

Commit 14c7956

Browse files
sbc100uysalibov
authored andcommitted
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 70e1808 commit 14c7956

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

src/library_sdl.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -828,13 +828,16 @@ var LibrarySDL = {
828828

829829
lookupKeyCodeForEvent(event) {
830830
var code = event.keyCode;
831-
if (code >= 65 && code <= 90) {
831+
if (code >= 65 && code <= 90) { // ASCII A-Z
832832
code += 32; // make lowercase for SDL
833833
} else {
834+
// Look up DOM code in the keyCodes table with fallback for ASCII codes
835+
// which can match between DOM codes and SDL keycodes (allows keyCodes
836+
// to be smaller).
837+
code = SDL.keyCodes[code] || (code < 128 ? code : 0);
834838
#if RUNTIME_DEBUG
835-
if (!(event.keyCode in SDL.keyCodes)) dbg('unknown keyCode: ', event.keyCode);
839+
if (!code) dbg('unmapped keyCode: ', event.keyCode);
836840
#endif
837-
code = SDL.keyCodes[event.keyCode] || event.keyCode;
838841
// If this is one of the modifier keys (224 | 1<<10 - 227 | 1<<10), and the event specifies that it is
839842
// a right key, add 4 to get the right key SDL key code.
840843
if (event.location === 2 /*KeyboardEvent.DOM_KEY_LOCATION_RIGHT*/ && code >= (224 | 1<<10) && code <= (227 | 1<<10)) {
@@ -859,6 +862,8 @@ var LibrarySDL = {
859862
case 'keyup': {
860863
var down = event.type === 'keydown';
861864
var code = SDL.lookupKeyCodeForEvent(event);
865+
// Ignore key events that we don't (yet) map to SDL keys
866+
if (!code) return;
862867
#if !SAFE_HEAP
863868
// Assigning a boolean to HEAP8, that's alright but Closure would like to warn about it.
864869
// TODO(https://github.com/emscripten-core/emscripten/issues/16311):
@@ -943,6 +948,8 @@ var LibrarySDL = {
943948
dbg(`received ${event.type} event: keyCode=${event.keyCode}, key=${event.key}, code=${event.code}`);
944949
#endif
945950
var key = SDL.lookupKeyCodeForEvent(event);
951+
// Ignore key events that we don't (yet) map to SDL keys
952+
if (!key) return false;
946953
var scan;
947954
if (key >= 1024) {
948955
scan = key - 1024;

test/browser/test_sdl_key.c

Lines changed: 2 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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)