April 21, 2009

Apple Keyboard on Linux

Filed under: Technical — Tags: — James Bunton @ 5:20 pm

The Apple aluminium keyboards are very nice. I recently bought one for an Ubuntu Linux machine, and it requires some special configuration to work as expected.

This post describes how to fix the function keys and swap command (windows or ‘super’) keys with the alt (or option) keys.

Apple Aluminium Keyboard

The new modprobe/hid_apple way

The following commands will alter the parameters for the hid_apple module and then reload the module. You must run them as root.

# echo options hid_apple fnmode=2 swap_opt_cmd=1 > /etc/modprobe.d/hid_apple.conf
# rmmod hid_apple && modprobe hid_apple

The old xmodmap way

UPDATE 2014-11-11 – On new kernels you really should use the method above! It’s much better, you can use both Apple and non-Apple keyboards at the same time for example.

This command will fix the function keys, it saves you pressing fn-F1 whenever you want F1. The first command is for older kernels, the second is for version 2.6.28 or later.

# echo 2 > /sys/module/hid/parameters/pb_fnmode
# echo 2 > /sys/module/hid_apple/parameters/fnmode

Then add that line to your /etc/rc.local file, somewhere before the exit 0 at the end, so that it gets run on startup.

Next to swap the Command/Alt keys using xmodmap. I’m aware you can do this from the Gnome Keyboard Settings panel, but I’ve found this method works better. Particularly when combined with synergy.

Create a file called ~/.xmodmaprc with this inside:

clear mod1
clear mod4
keycode 133 = Alt_L
keycode 134 = Alt_R
keycode 64 = Super_L
keycode 108 = Super_R
add mod1 = Alt_L Alt_R
add mod4 = Super_L Super_R

Now run to activate the new keys, run:

$ xmodmap ~/.xmodmaprc

Don’t forget to add it to your list of startup programs. If you’re using Gnome, look at System->Preferences->Startup Applications
Otherwise you can just add it to ~/.xsession

To find the keycodes above I used the xev program. Try running it from a console. It shows you all X11 events that the xev window receives, including key presses/releases.


Greg Darke says:

It seems that under newer kernels (I am using 2.6.28), you need to change the file in /sys that you change. You must now use “echo 2 > /sys/modules/hid_apple/parameters/fnmode”

It also seems to require a different xmodmaprc file. Mine is:

remove mod1 = Alt_L Alt_R
keycode 64 = Super_L
keycode 133 = Alt_L
keycode 134 = Alt_R
keycode 108 = Super_R
add mod1 = Alt_L Alt_R
Colin Levy says:

Ah! I found this a couple years ago when I was using Linux for the first time and it helped me then. Setting up a new system now and I’m doing it again, using the xmodmaprc in the comments. Thanks for the awesome post!

andrej says:

After fighting with bugs:
X Error of failed request: BadValue (integer parameter out of range for operation)
Major opcode of failed request: 118 (X_SetModifierMapping)
Value in failed request: 0x17
Serial number of failed request: 17
Current serial number in output stream: 17

I found this page:

and realized that I need to use this:

clear Mod1
clear Mod4

! clear some mess coming out from ?
keycode 204 = NoSymbol NoSymbol NoSymbol NoSymbol
keycode 205 = NoSymbol NoSymbol NoSymbol NoSymbol
keycode 206 = NoSymbol NoSymbol NoSymbol NoSymbol
keycode 207 = NoSymbol NoSymbol NoSymbol NoSymbol

! Add keycodes as shown by xev
keycode 64 = Super_L Super_L Super_L Super_L
keycode 108 = Super_R Super_R Super_R Super_R
keycode 133 = Alt_L Alt_L Alt_L Alt_L
keycode 134 = Alt_R Alt_R Alt_R Alt_R

add Mod1 = Alt_L Alt_R
add Mod4 = Super_L Super_R

Thanks a lot for this great page!

Comments are closed.