Skip to content

Commit

Permalink
wayland: Look up pressed keys via keycodes instead of scancodes on ke…
Browse files Browse the repository at this point in the history
…yboard entry

On window focus, look up the pressed modifier keys via keycodes instead of scancodes to handle the case of remapped keys, as xkb remapping changes the associated keycode, not the scancode.
  • Loading branch information
Kontrabant authored and slouken committed Dec 14, 2022
1 parent 3a940ba commit e9a9afa
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions src/video/wayland/SDL_waylandevents.c
Original file line number Diff line number Diff line change
Expand Up @@ -1160,21 +1160,6 @@ static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
uint32_t serial, struct wl_surface *surface,
struct wl_array *keys)
{
/* Caps Lock not included because it only makes sense to consider modifiers
* that get held down, for the case where a user clicks on an unfocused
* window with a modifier key like Shift pressed, in a situation where the
* application handles Shift+click differently from a click
*/
const SDL_Scancode mod_scancodes[] = {
SDL_SCANCODE_LSHIFT,
SDL_SCANCODE_RSHIFT,
SDL_SCANCODE_LCTRL,
SDL_SCANCODE_RCTRL,
SDL_SCANCODE_LALT,
SDL_SCANCODE_RALT,
SDL_SCANCODE_LGUI,
SDL_SCANCODE_RGUI,
};
struct SDL_WaylandInput *input = data;
SDL_WindowData *window;
uint32_t *key;
Expand Down Expand Up @@ -1203,14 +1188,21 @@ static void keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,

wl_array_for_each (key, keys) {
const SDL_Scancode scancode = Wayland_get_scancode_from_key(input, *key + 8);

if (scancode != SDL_SCANCODE_UNKNOWN) {
for (uint32_t i = 0; i < sizeof mod_scancodes / sizeof *mod_scancodes; ++i) {
if (mod_scancodes[i] == scancode) {
SDL_SendKeyboardKey(0, SDL_PRESSED, scancode);
break;
}
}
const SDL_KeyCode keycode = SDL_GetKeyFromScancode(scancode);

switch (keycode) {
case SDLK_LSHIFT:
case SDLK_RSHIFT:
case SDLK_LCTRL:
case SDLK_RCTRL:
case SDLK_LALT:
case SDLK_RALT:
case SDLK_LGUI:
case SDLK_RGUI:
SDL_SendKeyboardKey(0, SDL_PRESSED, scancode);
break;
default:
break;
}
}
}
Expand Down

0 comments on commit e9a9afa

Please sign in to comment.