Apple Keyboards in Linux
April 19, 2008
Finally! After a year of cramping, odd hand contortions, and memory holes (ok not really the keyboards fault), I finally got my Apple keyboard to behave as I’d like.
Macintosh users are a peculiar breed with their FN keys and all. And whats with the Apple key? Linux and Windows users that try an Apple keyboard must face a keyboard that is controlled by the Apple Desktop Bus. The legendary Apple Desktop Bus (ADB) was used until 2005 before Apple moved to a USB desktop bus for it’s MacBooks and MacBook Pros. The unique desktop bus hardcodes function keys (only F1 - F6 though) as always on. This is a behavior that I’m unaccustomed to in Linux. After dealing with this for way too long, I have learned how to stop my left hand from cramping and have gotten the left side of my keyboard back, this is how I got it done:
Problems FN’ing
“The Mac’s keyboard layout is a little different from a standard PC one.”[1]
Learning the ADB keyboard is a pattern that can take time to acquire for the regular Windows or Linux users. The always on F1-F6 function keys always felt like a butler bringing Grey Poupon to me.
There are a couple easy options to change the ADB function key behavior: Powerbook buttons daemon(pbbuttonsd) provides an option to do this, but it has a couple odd bugs for me. Also lucky Intel-macs owners have a USBhid switch in /sys that can be easity triggered. Ahh, but this laptop is before that time. Therefore, the only way for me to go was to create a new keymap.
“I am the gatekeeper”
In Linux, key mappings are initially loaded for console with an init script. The script uses the loadkeys program to bind a new keymapping. By default many distrobutions and architechtures use the i386 profile console keymappings. Most keyboards are alike so this is acceptable, but obviously this mapping doesn’t have support for ADB keys. Macintosh keymaps are available but I’ve tried them and they are either depracated or for Old World Macs.
Keymaps are stored in /usr/share/keymaps, and are set using KEYMAP=”us” or KEYMAP=”ru-win” in the /etc/conf.d/keymaps init script configuration file. To load a keymap is easy enough:
loadkeys us loadkeys uk ...
Test and pick a keymap that has the highest number of correct key mappings for the keyboard. The more keys that are correct the less the keymap settings for the X server will have to be corrected as they inherit these settings.
Warning:Don’t change /etc/conf.d/keymaps and restart the init script (/etc/init.d/keymaps) if the X server is running! It’s best to work from console with no X server going. If this isn’t possible using loadkeys in X seems to be ok but it has the potential to create an unstable OS, edit as need be and restart the computer.
When a good keymap is found print it to file it so it can be edited.
dumpkeys > /etc/default.kmap
The program showkeys can be used to get the keycodes. For my computer I had to find the values of function keys F1-F6 when fucntion is both on and off.
Off On F1 59 224 F2 60 225 F3 61 114 F4 62 115 F5 63 reserved num-lock F6 64 113
For each of the keys I switched all instances of their keycodes in /etc/default.kmap. I also added Control to the Apple key:
Keycode 125 = Control
Finish by referencing the new keymap to the KEYMAP variable in /etc/conf.d/keymaps. The keymaps boot script or the system will need to be restarted.
X Got Served
With a default.kmap defining the key mappings most will retain their settings… but not always all. For example, the Apple key doesn’t retain control.
The X server configuration file will need to be set up properly too. For most users it is best to use the “macintosh” keymap. Edit the keyboard section in /etc/X11/xorg.conf with the model along with whatever appropriate country code:
Option "XkbModel" "macintosh"
Option "XkbLayout" "us"
For the remapping special keys (Control, Alt, Apple, Caps…) I’ve seen many people use xmodmad (discussed next section). Xmodmap is a good program but it can be quirky at times. A better way to specify these keys is in the Xserver configuration file. These key definitions can be specified with the “XkbOptions” tag in ~/etc/X11/xorg.conf
The X server has a list of known keyboard mappings in /usr/share/X11/xkb/rules/xorg.lst. From there I was able to map the Control key to Apple:
Option "XkbOptions" "altwin:ctrl_win"
Gnome Bully
Gnome shouldn’t even anything to do with setting the keyboard unless it can properly edit the /etc/X11/xorg.conf which it doesn’t. Keyboard mappings belong to the console and X server respectively. We discussed this before. If an error occurs that Xserver’s keyboard settings dont’ matching Gnome’s:
“The X system keyboard settings differ from your current GNOME keyboard settings”
delete Gnome’s keyboard gconf settings and it’s backup:
rm -r ~/.gconf/desktop/gnome/peripherals/keyboard/kbd/
As of Gnome 2.20 brightness up and down, volume up and down, and mute should all be mapped to the Xservers corresponding values: XF86BrightnessAdjust, XF86AudioLowerVolume, XF86AudioRaiseVolume, XF86AudioMute. Use xev to discover if these values are correct and set the appropriate keymap in /etc/X11/xorg.conf.
Mapping Other Keys
Not all keymappings can be defined in the X server configuration file, this is what xmodmap is good for.
First use xev to discover what values the keys are mapped to. Xev will be able to read keycodes as well as X server special settings: XF86AudioRaiseVolume, XF86AudioMute…
Forget about using the ~/.xmodmap file when using xmodmap. I found that it doesn’t always work. Xmodmap works better remapping through the program and then writting bash script to repeat these actions. Remapping keys with xmodmap can be done by:
xmodmap -e 'keycode 115 = Control_L'
Where “keycode 115″ is the key to map to left control key. Xmodmap can change, replace or swap just about all keybindings. Remapping may not be enough though and may work better if conflicting bindings are first removed and then reset:
xmodmap -e 'remove control = Control_L' xmodmap -e 'keycode 115 = Control_L' xmodmap -e 'add control = Control_L'
Eventually though some program will call on Xserver keymappings or perhaps binding have an expiration time and the xmodmap settings wil be lost. This is where xkbset comes in. It takes away the expiration values for keymappings.
xmodmap -e 'remove control = Control_L' xmodmap -e 'keycode 115 = Control_L' xmodmap -e 'add control = Control_L' xkbset exp m
The exp argument turns expiration off. Put this in a bash script and add it to startup and the keys will be remapped.
- For Gentoo users, there is an xkbset ebuild in bugzilla.<
Getting About an ADB/Gnome Bug
Though xev displayed that XF86AudioRaiseVolume, XF86AudioMute were mapped correctly, Gnome failed to be able to connect to them. This is likely an issue with ADB. Going to “Keyboard Shortcuts” and and re-entering the values re-enabled the funtion keys but it wasn’t very convient. So I got gconftool-2 to do the job:
gconftool-2 --type string --set /apps/gnome_settings_daemon/keybindings/volume_down XF86AudioLowerVolume gconftool-2 --type string --set /apps/gnome_settings_daemon/keybindings/volume_up XF86AudioRaiseVolume gconftool-2 --type string --set /apps/gnome_settings_daemon/keybindings/volume_mute XF86AudioMute
Notes:
- Xserver has controls to adjust the brightness up and down but Gnome Power Manager will need to be installed for the widget to be seen.
Resources
1 Comment Add your own
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed




1.
Blog do Enderson! »&hellip | May 29, 2008 at 2:50 pm
[...] Control pela tecla Command (maçã). Eu ainda não configurei tudo isso, mas você pode seguir este link que [...]