!!!Maintenance in Progress!!!

Re-mapping keys on OS X

March 17, 2005

Introduction

There are two two sets of keys which one might want to re-map on an OS X machine: the alphanumeric and symbolic (comma, asterisk etc.) keys and the modifier keys (Command, Option, Control, Esc etc.). A third type of modification is to customise keyboard shortcuts (beyond what is possible using Apple’s Preferences system). This article discusses all three in turn.

Remapping the alphanumeric and symbolic keys is possible, and reasonably straightforward, and a method to do this is explained below. OS X allows certain modifier keys to be re-mapped (see System Preferences→Keyboard and Mouse→Keyboard→Modifier Keys). As of OS X 10.4.8, only Caps Lock, Control, Option, and Command can be re-mapped (to one another); however it is possible to re-map some of the other modifier keys, and a simple way to do this is explained below. There used to be a kernel extension (which tend to be rather risky things) called uControl, which I understand allowed arbitrary re-mapping, but this is no longer maintained. There is what seems to be an immature program called fKeys, which the uControl developers point to as a possible successor, but it does not appear to be very simple or stable.

Finally, it is also possible to change the keyboard shortcuts (outside what OS X’s System Preferences allows).

Remapping Alphanumeric/Symbolic Keys

You can re-map (redefine) the keyboard for OS X. Apple provide instructions on how to do it here. However, the XML format is pretty complex (if you just want to make changes quickly), but there is an excellent piece of software called Ukelele, which can be downloaded for free. The software comes with an excellent manual (the most well-written of any freeware I’ve seen for quite some time!).

Remapping Modifier Keys

The following is based on the information at macosxhints, but presents the information in what I hope is a clearer manner, and uses a less complex method of making the required changes.

OS X modifier keys have numeric codes. A re-mapping is achieved by specifying a source code (i.e. the key you want to change the meaning of) and a destination code (i.e. what the key should do instead). The key names and their codes are given below:

Note that kernel panic is not a key! Apparently if you set this code as a destination, you will cause your Mac to crash.

To edit or create a new mapping, follow the instructions below:

  1. Use Spotlight to find the program “Property List Editor”, and run it.
  2. Choose File→Open and navigate to Library/Preferences/ByHost/ (i.e. the one in your home directory, not the system-level one).
  3. Use the Spotlight search box in the file open dialog box to search the above directory for the hidden file called something like .GlobalPreferences.XXXXX.plist (where XXXXX is some long alphanumeric string, which appears to be specific to particular computers or users); then open the file and save it as a backup somewhere on your computer. Then reopen the original file.
  4. Expand Root, then com.apple.keyboard.modifiermapping.
  5. The numbers 0–7 (or whatever) do not appear to have any particular meaning; they just list the mappings. You can see how a mapping is specified by expanding one of these numbers. The source and destination key codes are then easily seen.
  6. You can then simply edit a mapping by specifying a new destination key code, or create a new mapping by creating a child node of com.apple.keyboard.modifiermapping, and then creating a pair of source and destination nodes using exactly the same property name, class and the relevant key codes.
  7. Save the property list and log out and then back in the activate your changes.

Modifying Keyboard Shortcuts

There is a very useful article at the TextMate blog which a method of mapping OS X key combinations to events which OS X can handle (Cocoa events), should you want to (re)define your own keyboard shortcuts outside of what is allowed by System Preferences. A complete-looking list of default text-editing keyboard shortcuts for OS X may also be useful for reference.

An example keyboard layout

I have produced a Keyboard layout for OS X which I have called “Lambda” (after the lambda calculus, which I know almost nothing about), because the layout is “optimised” for programming and writing in English. I use the quotes because the layout is based on the QWERTY layout (I guess I’d use something based on the Dvorak layout if I really wanted an optimal keyboard layout). The layout does the following (based upon the Apple iBook keyboard shipped in the UK and probably the US):

I have also produced an OK-ish (for a first attempt) icon for the input mode for the OS X menu bar. By placing the icon file in the same directory as the .keylayout XML file, the icon is used to represent the layout. You can download each file below: