Re mapping the caps lock key in OS X

I have heard tales for a while of the joy of having your caps lock key remapped to a more useful modifier key. For me the most useful modifier key that I know of is ESC, for vim. Alas, this seems to be very dificult to do in OS X. Here are some of the false starts I made when trying to do this.

.vimrc
in vim you remap keys with the following command
:mmap
this is great and a very useful feature, unfortunately I can’t directly map the caps lock key with mmap.

System Preferences
at least in 10.4, OS X lets you remap the caps lock key to Control, Command, Alt, or Nothing. This is great for most people, but it doesn’t help me make it ESC. It also doesn’t help me map it to ESC for one app (Vim) and nothing for other apps.
You can find this option in
System Preferences > Keyboard & Mouse > Modifier Keys

.inputRC / xmodmap
These are files that BASH (maybe all shells) and X11 read on startup to map keys. I didn’t examine them too much. From what I hear it is possible to remap the Caps Lock key with xmodmap, but I don’t run X11 regularly

Ukelele / .keylayout files
Ukelele is a program for creating key layout files, it comes highly recommended, I wasn’t impressed. I found it easier to hand edit the xml key layout files that OS X uses. You can read about their schema here. I found out that the caps lock key should generated a key code of 55 or 57 depending on whether it’s being turned on or off. On OS X they don’t generate that key code for a keylayout file to manipulate.

Writing A C program to modify keycodes
I ran across this code example OS X Internals and this code. Now I was on to something. I had one program that modifies key codes, and another one that traps the caps lock key (or any other modifier key being pressed. I was able to hack the code to generate a key code of “74″ (unused in the keylayouts I am using) every time the caps lock key is pressed. I was enormously satisfied with this.

5 Responses to “Re mapping the caps lock key in OS X”

  1. paddy says:

    lets see how askimet handles comment

  2. Ari says:

    Hi,

    Were you able to prevent the effect of the caps lock too? I was only able to generate a keypress event from that but still got the caps lock toggle. It doesn’t help the VIM usage if I get capital letters.

    Could you send your version of the code? Any tips where to add this so that it’s run on startup?

    Thanks!

  3. Ari says:

    Nevermind, got it working.

  4. benzo says:

    hi
    Would you mind to post your code (or to sent it at my email)?

    thanks

  5. [...] I looked into other solutions but had the same experience as this blogger – the only thing that really works is some C. [...]

Leave a Reply