Switch between keyboard layout based on input event

keyboard-layoutudevxkbxorg

I have a french and a US keyboard on my computer. I'm using awesome wm and have set everything so that it is easy for me to switch between keyboard layout. But I still have to do it myself.

Theoretically, it should be possible for the computer to understand from which keyboard the input event comes and use the layout associated with the keyboard. I have looked it up but found no good answer.
Is it possible to do so ?

I was thinking that I could write a short code analyzing keyboard event and do the switch, but :

  • it would run in parallel with the event handler so there could be concurrency problem (something like this);
  • it looks like a dirty way of doing it and I prefer a clean solution.

Thanks for your help

Best Answer

You could do this with by configuring your keyboards separately. For example I use US English layout on my laptop keyboard and have a Sun Type 6 USB keyboard with german layout and I have the following in my /etc/X11/xorg.conf.d/10-evdev.conf:

# Default configuration for all keyboards not handled explicitly
Section "InputClass"
     Identifier "evdev keyboard catchall"
     MatchIsKeyboard "on"
     MatchDevicePath "/dev/input/event*"
     Driver "evdev"

     Option "XkbRules"   "evdev"
     Option "XkbModel"   "pc105"
     Option "XkbLayout"  "us(altgr-intl),de,ru"
     Option "XkbOptions" "lv3:menu_switch,caps:hyper,compose:ralt,grp:rctrl_rshift_toggle,terminate:ctrl_alt_bksp"
EndSection

# Match the external keyboard by USB ID
Section "InputClass"
    Identifier "Sun Type 6"
    MatchIsKeyboard "on"
    MatchUSBID "0430:0005"

    Driver "evdev"
    Option "XkbRules"   "evdev"
    Option "XkbModel"   "sun(type6)"
    Option "XkbLayout"  "de"
    Option "XkbOptions" "caps:hyper,compose:menu,terminalte:ctrl_alt_bksp"
EndSection
Related Question