OS X Key Mapping
The 9 key on my Mac Book Pro died today right in time for a presentation that I need to use it for. It would have been a lot of effort to install all of the bits and pieces that I required for the presentation onto a different machine. The Apple Store in Regent Street told me that they couldn't just sell me a replacement keyboard and that I would have to make an appointment for a Genius Bar session for them to replace it for me. Considering that this was not going to happen today, I decided to just remap my keyboard so that I could sacrifice a useless key for the 9 and the open parenthesis that I need. Because this is easier said than done, I decided to describe how I did this in order to try and help anybody else who is pulling their hair out trying to remap the keyboard.
Introduction
This is a description of how I remapped a disused key to masquerade as the 9 key on a Mac Book Pro Core 2 Duo running OS X 10.5.3. The key that I opted to sacrifice is, which I can no longer type, because I'm writing this on that very laptop, is the key between ESC and TAB on a UK keyboard (± and §).
Googling this established that there doesn't seem to be any way to swap these keys around using any of the utility tools shipped with Leopard. There are a few third party tools that perform key mapping, both free and commercial but none of the ones I looked at did what I needed to do (Double Command, Keyboard Maestro), or just didn't work with Leopard (uControl, fKeys ).
If you knew the specification of a keyboard layout definition in Leopard, you could just could just find this file and edit it. Keyboard layouts are specified in XML files that end in .keylayout. On face value, nothing could be simpler, but first of all you need to know where to get an example file from, and then you need to know how to edit it correctly. The only reason why I know this is because I've gone through the pain of it all and have found a tool that does this for you in a straightforward fashion.
Enter Ukelele. This is a Keyboard Layout Editor for OS X 10.2 and later. It has an easy to use GUI that allows you to redefine your keyboard layout.
Using Ukelele
Apart from being an easy tool to use, when you download Ukelele (this article is based on version 1.8b6), be sure to read the manual that is shipped with it as a PDF. This is an extremely comprehensive document about how to remap your keyboard, so my best advice is to read this and read it well.
Without wanting to recite what is already well described in the documentation, I will outline the basic steps to redefine a new keyboard layout:
- Start Ukelele and create a new definition by choosing New under the File menu;

- Base your layout on a previously existing layout. You probably have a few keyboard layout XML files already in Leopard, if you knew where to find them that is. Luckily, Ukelele ships with a whole lot of layout definitions in the System Keyboards directory that you can use to base your new layout on. I chose to base my layout on System Keyboards/Roman/British.keylayout. After having done this, you will see the following layout:

Then all you have to do to is to highlight the symbol you want to map, and then drag and drop it on to the desired target key in the keyboard editor. Being able to drag and drop is a simple yet powerful aspect, because it allows you to grab a symbol that you would otherwise not have been able to type in. To get the shift variant, just hold down the shift key and drop the desired symbol in (provided your shift button still works);
Once you are done, save the new layout file to the Library/Keyboard Layouts in your home directory;
In System Preferences, go to International -> Input Menu, scroll through the keyboard types and you should see the new layout you created. In this example I called the layout British Hack because it was based on the standard British layout:

- After having done this, you should be able to choose the new layout in the top right corner of your screen:

- However, if you try to use it straight away, you will find that nothing works! So, you'll have to use your mouse to get you back to the old layout. To activate the new layout, you'll have to log out and log back in again.
After that, you'll have a brand new keyboard layout, which if you get used to it (e.g. by just swapping the key caps on the keyboard), can save you having to replace your keyboard.
As I said before, it really does pay to read the Ukelele manual, even if it is trivial to use the GUI. Hats off to the author of the software and the manual.

Reader Comments