Apple keyboards with Linux

PPC - How I Got the Left Side of My Keyboard Back

Warning: For the most part this post is not relevant anymore. I don’t have an Apple computer anymore so siphon through it as best as you and if you let me know the details, I’ll update it.

After a year of cramping, odd hand contortions, I finally got my Apple keyboard to behave as I’d like. Apple uses hardware/firmware that alters the behavior of the standard keyboard (e.g. remapping F1 to brightness down, assigning the Apple key…). Prior to 2000 used the Apple Desktop Bus (ADB) now I believe it uses the firmware to alter the default behavior of select keys. Here’s what you can do to adapt your Apple keyboard to more standard behavior.

Problems FN’ing

“The Mac’s keyboard layout is a little different from a standard PC one.”[1]

Adapting to the ADB keyboard’s layout can take time to acquire for the regular Windows or Linux users, but isn’t necessary. For the F1-F6 function keys the behavior can be altered.

For older new age Macs that use ADB, the powerbook buttons daemon (pbbuttonsd) can do this (most PPC distros already have this installed). You’ll need to edit ‘/etc/pbbuttonsd.conf’ to configure it. Alternative you might be able to use the graphical client gtkpbbuttons. For newer macs install pommed to get normal function key behavior back.

X got Served

The standard layout now days is pc105, you can put it in your xorg configuration file (/etc/X11/xorg.conf) like this (this though likely isn’t necessary as evdev does a good job of detecting devices and language):

Section "InputDevice"
    Identifier  "Generic Keyboard"
    Driver      "kbd"
    Option      "XkbRules"  "xorg"
    Option      "XkbModel"  "pc105"
    Option      "XkbLayout" "us"
EndSection

For Old World Macs you might want to consider using “macintosh” instead of “pc105”.

Remap Command Keys

For the remapping special keys (Control, Alt, Apple, Caps…), I’ve seen many people use xmodmad (discussed in next section). Xmodmap is a good program but it can be quirky at times. A better way to specify these keys is in the xorg server configuration file.

The X server has a list of known keyboard mappings in /usr/share/X11/xkb/rules/xorg.lst. From there you can, for example, map the Control key to the Apple key:

Section "InputDevice"
    Identifier  "Generic Keyboard"
    Driver      "kbd"
    Option      "XkbRules"   "xorg"
    Option      "XkbModel"   "pc105"
    Option      "XkbLayout"  "us"
    Option      "XkbOptions" "altwin:ctrl
EndSection

Mapping Unknown Multimedia Keys

All brightness, volume keys… should work if mapped to the Xservers corresponding values: XF86BrightnessAdjust, XF86AudioLowerVolume, XF86AudioRaiseVolume, XF86AudioMute… Use xev to discover if these values are correct and set the appropriate keymap.

Other Key Mappings

You can create a special ~/.xmodmap that will load when xmodmap does but I found that it can sometimes be bugggy. Xmodmap works better directly 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 might try to define it’s own keymappings and the xmodmap settings wil be lost. This is where xkbset comes in. It takes away the expiration values for keymappings.

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.

Gnome Quirks

Warning: This may be an old bug and not relevant anymore.

Gnome shouldn’t have anything to do with keyboard settings. Be sure you got your setup correct (language and keyboard) and Gnome should use these values. Occasionally errors can occur though if you change your keyboard settings from within Gnome. If you get errors like:

The X system keyboard settings differ from your current GNOME keyboard settings

Deleting Gnome’s keyboard gconf settings should fix the problem:

rm -r ~/.gconf/desktop/gnome/peripherals/keyboard/kbd/

ADB Bug

Though xev displayed that XF86AudioRaiseVolume, XF86AudioMute were mapped correctly, Gnome failed to be able to connect to them (be sure Gnome Power Manager is installed first). This is likely an issue with ADB. Going to “Keyboard Shortcuts” and re-entering the values does work, but it’s easy just to create a script and load it on boot:

#!/bin/bash

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

Console

Warning: This section is dated and may not work anymore.

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 test a keymap:

loadkeys us
loadkeys uk
...

Test and pick a keymap that has the highest number of correct key mappings for the keyboard.

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 running.

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.

12 thoughts on “Apple keyboards with Linux

  1. Pingback: Blog do Enderson! » Blog Archive » Teclado Apple com Ubuntu

  2. Elias Hickman

    I just read this post and am trying to map some of my keys (key combinations) with my new mac (aluminium) keyboard.
    I’m using Fedora 11 and tried to follow this post to the letter. However, I’m still having some problems.
    I did:
    dumpkeys > /etc/default.kmap
    then edited it, changing portions (the F1-F19 keys). I got the values for the keys using:
    showkey
    I then tried to get the xorg.conf file working (I think this may be a problem) but was unable to get it working. I added your:
    Option “XkbModel” “macintosh”
    Option “XkbLayout” “kr” (I changed this bit)

    I deleted the rm -r ~/.gconf/desktop/gnome/peripherals/keyboard/kbd/
    Finally gnome asked me if I wanted to use the ~/.xmodmap, which was really just a renamed copy of /etc/default-mac.kmap (I don’t know if this needs to stay “default.kmap” ??).
    It loaded the file then nothing. I think there may be errors in the file. It looks like a mess in there. So here is a portion of the file if you wouldn’t mind looking at it.

    keycode 58 = Caps_Lock
    keycode 224 = F1 F13 Console_13 F25
    alt keycode 224 = Console_1
    control alt keycode 224 = Console_1
    keycode 225 = F2 F14 Console_14 F26
    alt keycode 225 = Console_2
    control alt keycode 225 = Console_2
    keycode 120 = F3 F15 Console_15 F27
    alt keycode 120 = Console_3
    control alt keycode 61 = Console_3

    The F1 key has “F13” next to it. I don’t know why, do you? Also it’s not allowing me to switch to other virtual consoles, with control+alt+F1. That feature along with print screen and a more convenient way to switch between “Korean” and “English” would be great. Any ideas?

    Reply
  3. Gen2ly

    Hello Elias, and thanks for reading. Haven’t updated so here’s an update.

    I once read that keymaps /usr/share/keymaps are inherited by the xorg server. I believe this information to either have now been wrong or is legacy. Xorg server keymappings are done in /usr/share/X11. Xorg server mappings are done separately from console, and now use evdev for detection. Ok, that’s probably more than you wanted to know. :). So the quick is you probably can’t use console mappings for, in fact, I’m pretty sure you can’t. As for your xorg server keymap, I’d personally use “pc105” instead of “macintosh” as it is becoming the standard now days, “kr” should be good for language, and remap the Apple key to control if you like. For remapping F1 to F1 and Apple(Cntl) + F1 to brightness type things… I believe that Macs still use pbbuttons, in fact I bet you got it installed. Look at /etc/pbbutonsd.conf. Here you can change keymappings for Apple specific keys. Theres a gui client too but it might be a bit dated called gtkpbuttons. The post was a bit old (I’m updating is as we speak) and for a first generation ibook that gave my troubles with pbbuttons, so thanks for being patient. Hope this helps.

    Reply
  4. Thales Oliveira

    Are you using the apple keyboard yet? Works fine? Wich ubuntu’s version are you using?
    I want to buy one, but I am afraid of if it is a really good idea. Do you recomend?

    Thanks

    Reply
  5. Gen2ly

    Yeah, I’ve used Apple keyboards quite a bit. Not using one right now though, Thales. Apple makes good products, keyboards though are a bit… expensive. I do like them though.

    Reply
  6. Gen2ly

    Yikes…. sure! There are a good number of them. I got a basic run on the mill one for $15 dollars, now. Does me just fine.

    Have a good day!

    Reply
  7. Chrisg

    Hi !

    tried

    xmodmap -e ‘remove control = Control_L’
    OK
    xmodmap -e ‘keycode 115 = Control_L’
    OK
    xmodmap -e ‘add control = Control_L’
    “X Error of failed request: BadValue (integer parameter out of range for operation)
    Major opcode of failed request: 118 (X_SetModifierMapping)”

    ? :)

    Reply
  8. Chrisg

    yes !!!! I’ve found why ! so happyyyyyy lol
    you have to remove the keysym ‘Control_L’ from all modifiers first :
    see where he is : xmodmap -pm
    remove : xmodmap -e ‘remove whereItIs = Control_L’
    finally
    xmodmap -e ‘add control = Control_L’ works

    long life to Open Source & freedom & Linux !

    Reply
  9. Hal68k

    Have you ever gotten capslock to act as an additional control key (in the tty)? Whenever I modfiy my keymap or load the emacs/emacs2 keymap I end up with a dead capslock key (which is still an improvement)

    Reply

Leave a comment