We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 3050

Summary: Linux key events do not respect keyboard layout
Product: SDL Reporter: Drew Bliss <drewb>
Component: eventsAssignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: jwyatt, metalcaedes
Version: HG 2.0Keywords: target-2.0.4
Hardware: x86_64   
OS: Linux   
Attachments: Test patch to check if fixing the group fixes the issue.

Description Drew Bliss 2015-07-06 17:40:57 UTC
As discussed over email it doesn't seem like changing keyboard layouts on Ubuntu 12.04 affects the keys returned from SDL.  Switching from an English keyboard to French keyboard, which changes z to w, has no effects on the keycodes (it does affect the actual character event).
Comment 1 Daniel Gibson 2015-07-19 23:10:14 UTC
you mean without restarting the game?
or even after restarting it after changing the keyboard layout?
Comment 2 Drew Bliss 2015-07-20 17:50:07 UTC
This is with changing the layout before the game has started.  Ryan investigated and it looks like the underlying system is translating keys in a way that isn't easily visible to SDL so at the moment this looks like a won't-fix.
Comment 3 Jason Wyatt 2015-08-14 14:59:24 UTC
I'm seeing what looks to be the same bug in Fedora 21. For me, the issue seems to be when multiple layouts are set-up, SDL always ends up using the first one, regardless of which is currently selected.

This seems to be caused by using group 0 in X11_XkbKeycodeToKeysym in X11_KeyCodeToUcs4. Hackily adding an XkbGetState to retrieve the current group, and using that instead gives the correct results.
Comment 4 Jason Wyatt 2015-08-14 15:28:34 UTC
Created attachment 2239 [details]
Test patch to check if fixing the group fixes the issue.

Attached is the patch I used to check that specifying the current group is sufficient.
Comment 5 Ryan C. Gordon 2015-08-15 03:31:53 UTC
(In reply to Jason Wyatt from comment #4)
> Created attachment 2239 [details]
> Test patch to check if fixing the group fixes the issue.
> 
> Attached is the patch I used to check that specifying the current group is
> sufficient.

This looks good to me, so it's now https://hg.libsdl.org/SDL/rev/8a89b58895c1

Thanks!

--ryan.
Comment 6 Ryan C. Gordon 2015-08-15 04:43:11 UTC
Just made a further change here:

    https://hg.libsdl.org/SDL/rev/41ce18cb4b3b

Jason's fix solved the mystery, and SDL does the right thing with non-primary keyboard layouts on Ubuntu 12.04 (etc) now, but it doesn't notice if the layout changes at runtime unless the new layout is the first in the list...same problem as before.

Moving to the front of the list makes X send a MappingNotify event, which we handle already, and on newer Ubuntu releases, this event is sent if you choose a layout that isn't at the start of the list, too.

On 12.04, we get a PropertyNotify for XKLAVIER_STATE ("Klavier" is German for keyboard, this is a library that handles layout stuff). So my new patch notices that property and reinits our keymap info.

It works here, but all this explanation is to say: I have no idea what I'm doing, so please tell me if it works on Fedora 21, too, and if anyone knows if this was even close to a correct approach.

Thanks,
--ryan.