New #include <emscripten/key_codes.h> header for keyboard layout agnostic input

193 views
Skip to first unread message

Jukka Jylänki

unread,
Aug 30, 2017, 12:50:30 PM8/30/17
to emscripte...@googlegroups.com
Hi all,

games often want to receive input that is not disturbed by the
keyboard layout one is using, but map keys to physical locations on
the keyboard.

Browsers have this ability based on a standardized
"KeyboardEvent.code" field:
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code

That field is an English language human readable string that describes
the physical key that was pressed. In JavaScript using strings as
unique IDs for keys is probably not that bad because JavaScript has
convenient support to switch-case strings in the language
(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch),
however in C and C++ it can be very clumsy. Because of this, there is
now a new header file #include <emscripten/key_codes.h> that allows
one to convert these strings to an enum ID for easy switch case in
C/C++. See the pull request
https://github.com/kripken/emscripten/pull/5492 where this support
just landed.

I have a small favor to ask from anyone who is interested in testing.
It is known that there are some cross-browser differences in the code
fields that are reported, see
https://github.com/kripken/emscripten/pull/5492#issuecomment-326046667.
If you have multiple browsers on your system and a few minutes of
time, please fire up all your browsers, and then navigate to

https://s3.amazonaws.com/mozilla-games/test_keyboard_codes/test_keyboard_codes.html

and tap through all keys on your keyboard on each of your browser. Are
there physical keys that produce different results for the reported
DOM_PK_* codes in the prints (the 'code' field)? Are there physical
keys that were not recognized? Are there physical keys that produce
the same key code instead of producing an unique code? If so, please
post a summary to https://github.com/kripken/emscripten/pull/5492
describing the differences you found across browsers.

I am a bit wary of doing emulation to hide cross-browser
inconsistencies, but if there are clear patterns that we can uncover
with this kind of crowdsourcing exercise, there might be some good
fixes that make sense to do.

Please report the OS version, your keyboard layout (in case it
matters) and exact browser versions that you tested, and the differing
key results at https://github.com/kripken/emscripten/pull/5492.

Thanks!
Jukka

Floh

unread,
Aug 31, 2017, 12:58:45 AM8/31/17
to emscripten-discuss
Nice! I used the KeyboardEvent.keyCode field so far to get this sort of 'virtual key' input, but this field is marked as deprecated (https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent), so I'm happy that there's now a "proper" solution :)

I did a test run on OSX in Chrome, Safari and Opera, but didn't find anything which isn't already mentioned here: 


Cheers,
-Floh.
Reply all
Reply to author
Forward
0 new messages