56

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.

0

6 Answers 6

44

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
7
  • Hi there, I did this and it works great for everything but the arrow keys and other directional keys (which no longer work at all). It could be that the keyboard is one of those ergonomic ones that have funky key mappings...any idea how to get those working again?
    – btelles
    Nov 3, 2011 at 13:12
  • 10
    So what's the best way to set this configuration at boot instead of having to manually invoke setxkbmap? Jun 25, 2013 at 10:53
  • A little curious, why are there 2 ids(id=9; id=10) corresponding to the same USB keyboard device(LITEON Technology USB Keyboard)? I find 2 ids on my machine also(id=11; id=17), then which id should be used for setxkbmap -device?
    – rustyhu
    Dec 6, 2020 at 8:39
  • 1
    @RichardTurner I write my setxkbmap ... configuration into ~/.xprofile file, it works fine after I log in to my Xorg desktop environment. Maybe this is not the best way.
    – rustyhu
    Dec 6, 2020 at 8:43
  • 1
    @RichardTurner I tested on my Arch Linux and ~/.xsession should be a better place for setxkbmap ... configuration. Reference
    – rustyhu
    Dec 28, 2020 at 6:40
11

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

7
  • your late answer maybe merits a late comment ... you have Identifier as "Happy Hacking Kbd" in both ... I think that's probably a mistake, the first InputClass Identifier should probably be "Generic Keyboard"
    – Daniel
    Apr 16, 2017 at 17:00
  • 1
    @Daniel The identifiers are actually different, "Happily Hacked Keyboard" vs "Happy Hacking Keyboard", but only slightly :)
    – zanegray
    Apr 17, 2017 at 17:53
  • thanks for the correct! ... I blame my bad reading glasses :)>v
    – Daniel
    Apr 21, 2017 at 21:49
  • ... but this (for keyboard greatly working, also different multiple layouts to cycle through are possible!, like us,ru,ua on one and de,cz,ua on the second keyboard) change for me somehow killed my middle mouse button of my external USB mouse. Have to look for what the interference is ... Jul 26, 2019 at 3:48
  • I'd love if this solution worked for me, but it isn't working - maybe because I can't get the vendor id right? The command "lsusb -v | grep -e idVendor" gave me "0x09da A4Tech Co., Ltd." so I also tried various options (without hexcode, underscore instead of space, etc.). Then I tried the vendor id obtained via "xinput list" and "udevadm info" (A4TECH). I even changed the number 30 to 50 also because I have a file "/usr/share/X11/xorg.conf.d/40-libinput.conf" including a global keyboard configuration. But I couldn't get anywere. Any ideas please? (Ubuntu 18.04)
    – Sadi
    Jul 29, 2019 at 11:48
10

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.

5

It might be possible, judging by the -device argument to setxkbmap:

       -device device
               Specifies the numeric device id of the input device to be
               updated with the new keyboard layout. If not specified, the
               core keyboard device of the X server is updated.

Naturally, however, I do not have a second keyboard to try it at the moment.

-A Dvorak user

1
  • Hmph. -device 0 switches both, -device 2 (should be the on-board keyboard per Arkenklo's answer) doesn't switch anything, at least on my laptop and USB keyboard. Dec 4, 2009 at 0:19
1

I don't think you can do this exactly the way you want. There are two options that I know of:

1) You can make switching a simple mouse click / keystroke.

With Ubuntu / GNOME:

Mouse Click

In Ubuntu / Gnome, providing you have your input layouts setup, go to an empty spot on your panel, right click and click 'Add to Panel'. Select 'Keyboard Indicator' to add this to the panel. That should input a tiny little piece of text indicating your keyboard layout and you can click this to switch.

Keyboard shortcut

System -> Preferences -> Keyboard -> Layouts -> Layout Options

There's an option there "Key(s) to change layout". You can set it to Alt+Shift, which shouldn't disturb any normal keyboard shortcuts.

I presume there is a KDE way of doing that also.

2) The other alternative is http://en.wikipedia.org/wiki/Xneur, but I am not sure if this is available for Swedish and it may not be possible for languages which have similar constructs to English.

1

I have the same situation, a laptop with a Brazilian keyboard and a German external keyboard.

My solution was to add two setxkbmap calls, but the tricky part here is to add all the layouts you need on both calls:

setxkbmap -device 12 -layout de,br
setxkbmap -device 14 -layout br,de

Device 12 is the external german keyboard whille device 14 is the laptop's brazilian keyboard.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.