Skip to content

Commit 9221548

Browse files
committed
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 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 #2098.
1 parent 0ddec7e commit 9221548

File tree

3 files changed

+579
-325
lines changed

3 files changed

+579
-325
lines changed

src/events/SDL_keyboard.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -800,12 +800,11 @@ SDL_SetKeyboardFocus(SDL_Window * window)
800800
}
801801

802802
static int
803-
SDL_SendKeyboardKeyInternal(Uint8 source, Uint8 state, SDL_Scancode scancode)
803+
SDL_SendKeyboardKeyInternal(Uint8 source, Uint8 state, SDL_Scancode scancode, SDL_Keycode keycode)
804804
{
805805
SDL_Keyboard *keyboard = &SDL_keyboard;
806806
int posted;
807807
SDL_Keymod modifier;
808-
SDL_Keycode keycode;
809808
Uint32 type;
810809
Uint8 repeat = SDL_FALSE;
811810

@@ -851,7 +850,9 @@ SDL_SendKeyboardKeyInternal(Uint8 source, Uint8 state, SDL_Scancode scancode)
851850
/* Update internal keyboard state */
852851
keyboard->keystate[scancode] = state;
853852

854-
keycode = keyboard->keymap[scancode];
853+
if (keycode == SDLK_UNKNOWN) {
854+
keycode = keyboard->keymap[scancode];
855+
}
855856

856857
if (source == KEYBOARD_AUTORELEASE) {
857858
keyboard->autorelease_pending = SDL_TRUE;
@@ -971,13 +972,19 @@ SDL_SendKeyboardUnicodeKey(Uint32 ch)
971972
int
972973
SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode)
973974
{
974-
return SDL_SendKeyboardKeyInternal(KEYBOARD_HARDWARE, state, scancode);
975+
return SDL_SendKeyboardKeyInternal(KEYBOARD_HARDWARE, state, scancode, SDLK_UNKNOWN);
976+
}
977+
978+
int
979+
SDL_SendKeyboardKeyComplete(Uint8 state, SDL_Scancode scancode, SDL_Keycode keycode)
980+
{
981+
return SDL_SendKeyboardKeyInternal(KEYBOARD_HARDWARE, state, scancode, keycode);
975982
}
976983

977984
int
978985
SDL_SendKeyboardKeyAutoRelease(SDL_Scancode scancode)
979986
{
980-
return SDL_SendKeyboardKeyInternal(KEYBOARD_AUTORELEASE, SDL_PRESSED, scancode);
987+
return SDL_SendKeyboardKeyInternal(KEYBOARD_AUTORELEASE, SDL_PRESSED, scancode, SDLK_UNKNOWN);
981988
}
982989

983990
void
@@ -989,7 +996,7 @@ SDL_ReleaseAutoReleaseKeys(void)
989996
if (keyboard->autorelease_pending) {
990997
for (scancode = SDL_SCANCODE_UNKNOWN; scancode < SDL_NUM_SCANCODES; ++scancode) {
991998
if (keyboard->keysource[scancode] == KEYBOARD_AUTORELEASE) {
992-
SDL_SendKeyboardKeyInternal(KEYBOARD_AUTORELEASE, SDL_RELEASED, scancode);
999+
SDL_SendKeyboardKeyInternal(KEYBOARD_AUTORELEASE, SDL_RELEASED, scancode, SDLK_UNKNOWN);
9931000
}
9941001
}
9951002
keyboard->autorelease_pending = SDL_FALSE;

src/events/SDL_keyboard_c.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ extern int SDL_SendKeyboardUnicodeKey(Uint32 ch);
5353
extern int SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode);
5454
extern int SDL_SendKeyboardKeyAutoRelease(SDL_Scancode scancode);
5555

56+
/* This is for platforms that don't know the keymap but can report scancode and keycode directly.
57+
Most platforms should prefer to optionally call SDL_SetKeymap and then use SDL_SendKeyboardKey. */
58+
extern int SDL_SendKeyboardKeyComplete(Uint8 state, SDL_Scancode scancode, SDL_Keycode keycode);
59+
5660
/* Release all the autorelease keys */
5761
extern void SDL_ReleaseAutoReleaseKeys(void);
5862

0 commit comments

Comments
 (0)