Different keyboard layouts for different keyboards

Created: — modified: — tags: linux

On Linux, you can have different keyboard layouts for different physical keyboards. How?

Update (2025): Sway supports this natively. It's so easy it doesn't deserve its own blog post :)

On X you might try to add a line like this:

SUBSYSTEM=="input", ATTRS{name}=="Logitech USB Keyboard", ENV{DISPLAY}=":0", ENV{XAUTHORITY}="/home/youruser/.Xauthority", RUN+="/usr/bin/setxkbmap -device $(/usr/bin/xinput list --id-only 'Logitech USB Keyboard') us,ru pc105 ,winkeys grp:shift_caps_switch,grp_led:caps,terminate:ctrl_alt_bksp"

to your /etc/udev/rules.d/zz-local.rules file (create it if it doesn't exist).

When this blog post was originally written, this line used to work for me:

SUBSYSTEM=="input", ACTION=="add", ENV{ID_SERIAL}=="046a_0011", ENV{XKBLAYOUT}="us,ru", ENV{XKBVARIANT}=",winkeys", ENV{XKBOPTIONS}="grp:shift_caps_switch,grp_led:caps,terminate:ctrl_alt_bksp"

But now I wasn't able to find any docs or explanation to why it should work (and, as expected, it doesn't work).

Probably a better option would be to add a new file in /etc/X11/xorg.conf.d dir with a content like this:

Section "InputClass"
	Identifier "logitech-keyboard"
	MatchIsKeyboard "on"
	MatchProduct "Logitech USB Keyboard"
	Option "XkbLayout" "no"
	Option "XkbVariant" "nodeadkeys"
EndSection

Or use MatchUSBID "046d:0011" instead of MatchProduct for narower targeting.

But again, this is only for X.