Two keyboards on one computer. When I write with A I want a US keyboard layout, when I use B I want Swedish. Possible?

I have a laptop which I use an external keyboard with. When I type using the external keyboard I want to use a US keyboard layout, since that's the type of external keyboard I have. On the other hand, when I type using the integrated keyboard I'd like to use a Swedish layout.

Is this possible? And how would I go about doing it?

I'm asking here as I think we'll find most Linux expertise here.


Solution 1:

First, you have to find the device ID of the keyboard to change:

$ xinput -list | grep -i key
⎣ Virtual core keyboard                     id=3    [master keyboard (2)]
    ↳ Virtual core XTEST keyboard               id=5    [slave  keyboard (3)]
    ↳ Power Button                              id=6    [slave  keyboard (3)]
    ↳ Power Button                              id=7    [slave  keyboard (3)]
    ↳ LITEON Technology USB Keyboard            id=9    [slave  keyboard (3)]
    ↳ LITEON Technology USB Keyboard            id=10   [slave  keyboard (3)]

Then change the layout using setxkbmap:

$ setxkbmap -device 9 dvorak

Some versions of setxkbmap need a '-layout' argument:

$ setxkbmap -device 9 -layout dvorak

Solution 2:

I can positively say that what you want to do is indeed possible. I achieved the specified scenario by first

sudo cat /dev/input/event(0,1,2..)

while typing on each keyboard to ensure that they were recognized as separate devices. Then I simply applied trial-and-error, experimenting with

setxkbmap -device (1-x) us.

I discovered that setxkbmap -device 1 us configured both (all) keyboards, while setxkbmap -device 6 us only affected the secondary. Of relevance is perhaps that they both were connected via PS/2.

This of course is not guaranteed to work for you, and doesn't even classify as a proper answer, but it confirms that it's at all possible. I don't know according to which scheme the individual keyboards are named in /dev/input, but I'm sure that the names can be specified in udev, making it possible to put the appropriate setxkbmap command in /etc/rc.local or similar.

Solution 3:

Although this question is a tad old, I'd thought I'd share my solution since none of the other answers addressed how to have this work on boot.

My situation: I have a Happy Hacking Keyboard that plugs in via usb that has keys moved around (control where caps lock is, etc). I use this most of the time, but sometimes I would like to use the standard laptop keyboard or another usb keyboard. For these keyboards I created a custom keyboard layout.

The trick is that the custom keyboard layout should not be applied to the Happy Hacking Keyboard.

Solution: I created a Xorg config file as such:

$ cat /etc/X11/xorg.conf.d/30-keyboard.conf 
Section "InputClass"
        Identifier "Happily Hacked Keyboard"
        MatchDevicePath "/dev/input/event*"
        MatchIsKeyboard "on"
        Driver "evdev"

        Option "XkbLayout" "us"
        Option "XkbVariant" "hhk"
EndSection

Section "InputClass"
        Identifier "Happy Hacking Keyboard"
        MatchIsKeyboard "on"
        MatchVendor "Topre_Corporation"
        Driver "evdev"

        Option "XkbLayout" "us"
        Option "XkbVariant" "basic"
EndSection

The first part basically says for any standard keyboard, apply the us layout with the the custom variant. The second part says for the happy hacking keyboard, use the us layout with the basic variant.

You can match devices based on a bunch of parameters: xorg doc