Multiple Keyboard Layouts and Shortcuts

KDE has a very handy feature to switch keyboard layouts on the fly. This is for instance useful if you use the German keyboard layout by default, and the US layout for programming. To enable keyboard layout switching, go into System Settings > Input Devices (category Hardware) > Keyboard item > Layouts tab:

Here, ‘[x] Configure layouts‘ is checked, enabling the list view. I added the German keyboard layout, and then the English (US) layout. Notice also, that the shortcut is set to ‘Ctrl+Alt+K‘. Clicking apply, a tiny little indicator appears in the panel:

You now can quickly switch with Ctrl+Alt+K between the German and the US layout. Quite efficient, especially since the keyboard layout config page allows to switch the language on application basis.

Unchanged Keyboard Shortcuts

Switching the keyboard layout has one potential issue, though: The shortcuts remain unchanged. So if undo is mapped to Ctrl+z in the German layout, it is still mapped to Ctrl+z in the US layout. Note that by ‘z’ we refer to the hardware key on the keyboard. As a consequence, in the US layout, hitting the hardware key ‘y’ on the German keyboard inserts the character ‘z’, but the z in Ctrl+z is still on the hardware key ‘z’. This behavior may or may not be wanted.

Getting more into detail reveals that the order of the keyboard layouts in the first screen shot is of importance: If you first add the German ‘de‘ layout, and then the English ‘us‘ layout, then the shortcuts will always use the Germany keyboard layout, independent of what keyboard layout is chosen.

Reversely, if you first add the English ‘us’ layout, and then the German ‘de‘ layout, then the shortcuts will always use the English ‘us‘ keyboard layout.

So it seems that the order defines a priority, and the shortcuts always use the first entry in the list.

The correct solution to fix this would (in my humble opinion) be to add an option ‘[x] Shorcuts follow keyboard layout‘ or similar. But since this option does not exist, let’s do a quick hack to still get what we want here.

A Workaround

First we reset the shortcut in the settings of the keyboard layout options:

Click apply and close the dialog. Now, the shortcut ‘Ctrl+Alt+K‘ is unbound. Our idea is now to create a script that toggles the keyboard layout by calling setxkbmap with the appropriate parameters and bind this script via a global shortcut to ‘Ctrl+Alt+K‘.

To this end, we first have to create the script. So let’s first type `setxkbmap -query` in the console and check the output. For me, this results in:

$ setxkbmap -query
rules: evdev
model: pc101
layout: de,us
variant: nodeadkeys,

From this, we can follow that the current xkb layout is achieved with:

setxkbmap -model pc101 -layout de,us -variant nodeadkeys

Now, let’s switch the de,us to us,de and try the following:

setxkbmap -model pc101 -layout us,de -variant nodeadkeys

Notice, that the keyboard layout indicator in the panel switched to ‘us‘. Calling the first variant with de,us again, we get back to the German layout.

This discovery leads us to the following script switch-keyboard-layout.sh:

#!/bin/sh

# query xkb map: us,de -> us is primary; de,us -> de is primary
dummy=`setxkbmap -query | grep us,de`

# return value 0: us,de; return value != 0, de,us
if [ $? -ne 0 ]; then
  # de is primary, now make us primary in list
  setxkbmap -model pc101 -layout us,de -variant nodeadkeys
else
  # us is primary, now make de primary in list
  setxkbmap -model pc101 -layout de,us -variant nodeadkeys
fi

Save this script somewhere to switch-keyboard-layout.sh and make it executable with

chmod 755 switch-keyboard-layout.sh

Each time we execute this script, the keyboard layout is toggled.

Next, we go into System Settings again and navigate to Shortcuts and Gestures (Common Appearance and Behavior), there in the Custom Shortcuts we add a new Command/URL item named ‘SwitchKeyboardLayout’ as follows:

As a comment for this new item, we write ‘Switch Keyboard Layout‘, in the ‘Trigger‘ tab, bind the global shortcut to ‘Ctrl+Alt+K‘, and in the ‘Action‘ tab, choose the switch-keyboard-layout.sh script. Finally click Apply, and close the dialog.

Now, hitting Ctrl+Alt+K calls our script and correctly toggles the keyboard layout including the shortcuts.

Unfortunately, this approach does not support e.g. switching the keyboard layout on application basis as the switching policy of the Keyboard settings (first screen shot) allows. Still it works.

A final remark, though: For GTK applications this works out of the box. So is there any real reason why this is not the case for KDE / Qt applications? A real fix would be very much appreciated, I’d be also fine with an option. But not providing this feature at all is very thin ice…

Update: This issue was reported as KDE bug #197552 in 2009, and resolved as an upstream issue. However, it never was reported to Qt upstream. If I may say so, this is not how resolving bugs in KDE usually works. Grrr…

6 thoughts on “Multiple Keyboard Layouts and Shortcuts”

    1. I’m pretty sure this is not the real issue. According to the qt bug report above, this issue was fixed prior to Qt 4.8.5. I’ve tested Qt 4.8.1 it’s not fixed. It’s neither fixed in Qt 4.8.5, and I also checked out the Qt 4.8 branch which includes a revert of the respective commit that is supposed to lead to this issue: And even with the newest 4.8 branch it doesn’t work.

      So no, this is certainly not fixed, sorry.

  1. Hi Dominik,
    I’m doing the same – German layout for writing German texts, US layout for programming and the shell. I’m using caps lock to switch layouts. Concerning shortcuts: honestly, I don’t know if I need to type Ctrl-y or Ctrl-z to undo. Most of the time I probably try one or the other shortcut. While I have no problem with braces, brackets, and slashes, y/z gives me a constant headache. On the other hand, when writing English, I expect the z to be left of x, while when writing German, I expect it to be right of t. Strange, isn’t it? So far, there seems to be no good alternative.

    1. I can see the advantage of having fixed shortcuts all the time. But let’s face it: Say you want to change the keyboard shortcuts for a guest who is accustomed to another keyboard layout, you are doomed. No easy way around it. This is certainly a bug, not a feature. Really. It’s just inconsistent…

  2. The problem is that the shortcuts change in GTK apps (which counts LibreOffice, Firefox and Thunderbird, Pidgin, Gimp, etc) while they doesn’t change in Qt apps and SDL games. That’s really sad because it’s really practical to use an ergonomic keyboard layout (Dvorak, Colemak, Bépo, Neo…) but keeping qwerty or azerty shortcuts. Damn GTK.

    I don’t understand why some people use their local layout and US qwerty for programming. For example, French azerty or German qwertz are almost as efficient, if not more than qwerty for programming. If you really want to have a good programming layout, you can use programmer Dvorak, Neo (for German) or Bépo (for French). Colemak isn’t good for programming because it has the same characters position than US qwerty.

  3. I’m using a Latin American keyboard, and wanted to learn a Dvorak version that I made. For me, the shortcuts must follow the distribution in order to have a consistent experience.

    setxkbmap -query gives
    rules: evdev
    model: pc101
    layout: latam,latam
    variant: ,dvorak
    when normal Latin American is set and

    layout: latam,latam
    variant: dvorak,
    when Dvorak is

    I just needed to change grep for the variant part instead of the layout
    dummy=`setxkbmap -query | grep dvorak,`

    and the setxkbmap orders accordingly.

    Thanks.

Leave a Reply