I have a Mac, and the modifier keys on the keyboard are in order from left to right: Ctrl, Alt, Super (aka. Cmd, ⌘, Windows-key). I'd like the order to be Ctrl, Super, Alt instead, and so I have proceeded to remap them.

There's a rub, tough, because I regularly attach an external keyboard which has the keys in the correct order already. If I swap the keys here, they end up wrong.

So I want to remap/swap the keys for apple keyboards specifically. As a substitute for this, I'd gladly accept remaping the keys for the specific keyboard stuck in my laptop only (as opposed to all apple keyboards automatically).


After researching this, I have ended up with a solution that is only half working.

I am running the following script on login (~/.kde/Autostart/custom-remap.sh), which attempts to remap the keys for my internal keyboard only:

#!/bin/bash -e

DIRNAME=$(dirname $0)

# Use xinput to target a specific keyboard only:
APPLE_KEYBOARD_ID=$(
    xinput list |
    grep 'Apple Internal Keyboard' |
    sed -n 's/.*id=\([0-9]\+\).*/\1/p'
)

# Apply mappings from xkb/symbols/custom
# Maps ctrl, win and alt-keys to be in PC standard positions
setxkbmap -device $APPLE_KEYBOARD_ID -print |
    sed 's/\(xkb_symbols.*\)"/\1+custom(apple-keyboard-swaps)"/' |
    xkbcomp -I$DIRNAME/xkb -i $APPLE_KEYBOARD_ID -synch - $DISPLAY

This relies on the following file, xkb/symbols/custom:

xkb_symbols "apple-keyboard-swaps" {
    key <LWIN> { [ Alt_L ] };
    key <LALT> { [ Super_L ] };

    key <RWIN> { [ Alt_R ] };
    key <RALT> { [ Control_R ] };
};

This solution sometimes works perfectly, sometimes has no effect and recently it seemed like a hybrid result took root in my system: Neither of the Alt or Super keys would work properly, they seem to act a little bit as both.

"Sometimes" is really ill defined. I can get into either state after a reboot. Attaching/detaching my external keyboard might trigger a state change. Other times, seemingly at random, the state will switch while I'm using the computer. Running the script multiple times does not seem to affect the state (if it doesn't take the first time, it doesn't take the n-th time either).


How can I achieve the relevant remapping and have it working reliably?

(I am currently running Kubuntu 14.04, but it acted similarly under Kubuntu 13.10)

  • Just FYI… In theory, the correct way to do this is via xorg.conf, which should allow per-keyboard settings. However, this bug (at least in 13.10) prevented this from working. – Sparhawk Apr 16 '14 at 10:28
  • @Sparhawk It doesn't seem unlikely to me that KDE will override any such configuration in xorg.conf. My reasoning goes "xorg.conf will be considered first, and then later on, KDE will apply its keyboard configuration". Do you know anything about this? – Magnus Hoff Apr 16 '14 at 11:43
  • That's logical, but I actually think this is not the case. KDE doesn't mess with xkb configuration for me (perhaps this is why your settings are buggy?). I have the following settings. System Settings > Input Devices > Keyboard. Then, Layouts > uncheck Configure layouts. Also, Advanced > uncheck Configure keyboard options. – Sparhawk Apr 18 '14 at 17:04
  • Well, that's certainly a difference. I'm switching layouts all the time, and it does seem that enabling this makes KDE more prone to reverting my customizations. – Magnus Hoff Apr 19 '14 at 5:47
  • Glad to point you in the right direction. If it works, write it up as an answer, and accept it. (I feel it was a collaborative effort :) – Sparhawk Apr 19 '14 at 12:29
up vote 2 down vote accepted

I also wanted to do swap cmd and alt keys only on my MacBook Pro, but not when I have a normal external keyboard plugged in.

From the directions here, the following "just works" on 14.04 (and likely 13.10):

  1. Add options hid_apple swap_opt_cmd=1 to /etc/modprobe.d/hid_apple.conf, creating the file if it doesn't exist
  2. Rebuild initramfs with sudo update-initramfs -u -k all
  3. Reboot
  • (And thanks @magnus-hoff for the edit. I added a bit to it to flesh it out.) – kmsquire Oct 2 '14 at 6:51
  • This is so far completely stable, so marking as accepted. However, I think my original question points to a keymapping problem in KDE (?) that they should probably fix, and this issue is not yet resolved. Maybe somebody else will handle that :) – Magnus Hoff Oct 17 '14 at 12:54

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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