Skip to content

emscripten: Make an attempt at correct keyboard scancode/keycodes. #6364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 28, 2022

Conversation

icculus
Copy link
Collaborator

@icculus icculus commented Oct 9, 2022

emscripten: Make an attempt at correct keyboard scancode/keycodes.

This uses a newer browser API to get physical scancodes, but still uses the (deprecated) event field that we were already using scancodes, but for keycodes instead now, which appears to be more accurate.

Since keyboard layout isn't (generally) available to web apps, this adds an internal interface to send key events with both scancode and keycode to SDL's internals, instead of sending just scancodes and expecting SDL to use its own keymap to generate keycodes.

Future work in this area would be to use the keyboard layout APIs on browsers that support them, which would allow us to use SDL's usual keymap code and not rely on a deprecated browser API, but until we get there, this patch gives significantly more correct results than we would have before.

Fixes #2098.

This uses a newer browser API to get physical scancodes, but still
uses the (deprecated) event field that we were already using for
scancodes, but for keycodes instead now, which appears to be more
accurate.

Since keyboard layout isn't (generally) available to web apps, this
adds an internal interface to send key events with both scancode
and keycode to SDL's internals, instead of sending just scancodes and
expecting SDL to use its own keymap to generate keycodes.

Future work in this area would be to use the keyboard layout APIs
on browsers that support them, which would allow us to use SDL's
usual keymap code and not rely on a deprecated browser API, but
until we get there, this patch gives significantly more correct
results than we would have before.

Fixes libsdl-org#2098.
@Daft-Freak
Copy link
Collaborator

Daft-Freak commented Oct 12, 2022

If only there was a (standard) property for "with layout, but not modifiers" on key events...

Anyway, this does fix the scancode for the '#' key from KP_HASH/APOSTROPHE (firefox/chrome) to BACKSLASH on my UK keybaord, so seems to be an improvement. (keycode is still wrong, but that's an existing issue with .keyCode conflicts...)

Edit: Hmm, the \ key (which is now NONUSBACKSLASH) doesn't seem to generate TEXTINPUT events.

@icculus
Copy link
Collaborator Author

icculus commented Oct 28, 2022

If only there was a (standard) property for "with layout, but not modifiers" on key events...

I assume the solution to this will eventually be the Keyboard Layout Map API, which works in Chrome-based browsers but not Firefox or Safari yet. I definitely don't have time to mess with this right now, but it might be worth making the current code into a fallback for when that API isn't available (and eventually remove it entirely).

But for now, I'm merging this PR, as it seems to improve the current state of things.

@icculus icculus merged commit b9e1d1b into libsdl-org:main Oct 28, 2022
@icculus icculus deleted the emscripten-keyboard-improvements branch October 28, 2022 20:19
@sbc100
Copy link
Contributor

sbc100 commented Feb 14, 2024

I'm currently trying to roll this into emscripten and running into failure in the test_sdl2_key test: https://github.com/emscripten-core/emscripten/blob/main/test/browser/test_sdl2_key.c

The 2.24.2 the test is generating:

KEYDOWN: up
KEYDOWN: down
KEYDOWN: left
KEYDOWN: right
KEYDOWN: a
TEXTINPUT: A
KEYDOWN: b scancode
TEXTINPUT: B

With 2.26.0 (I assume due to this change) the test is generating:

TEXTINPUT: &
TEXTINPUT: (
TEXTINPUT: %
TEXTINPUT: '
TEXTINPUT: A
TEXTINPUT: B
TEXTINPUT: d

I'm hoping this is perhaps the test harness that needs updating in the way it sends fake key events? Hopefully this isn't a breaking change for users?

@sbc100
Copy link
Contributor

sbc100 commented Feb 14, 2024

Indeed, it looks like it was out fake keyboard events that were causing the issue. Fixed in emscripten-core/emscripten#21337

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Emscripten's key events should attempt to use the 'code' property.
3 participants