Thursday, March 6, 2008

Remap Caps Lock key for virtual console windows

My last blog entry explains how to use xmodmap to remap the Caps Lock key to the Escape key in X. That takes care of the keyboard mapping when you are in X. What about when you are in a virtual console window? You need to follow the steps below. Make sure that you sudo root before you execute the following commands.

  1. Find out the keycode of the key that you want remapped.

    Execute the showkey command as root in a virtual consolde:
    $ showkey
    kb mode was UNICODE

    press any key (program terminates after 10s of last keypress)...

    Hit the Caps Lock key, wait 10 seconds (default timeout), and the showkey command will exit on its own.
    $ showkey
    kb mode was UNICODE

    press any key (program terminates after 10s of last keypress)...

    The keycode for the Caps Lock key is 0x3a in hex, or 58 in decimal.

  2. Find out the symbolic name (key symbol) of the key that you want to map to.
    You can list all the supported symbolic names by dumpkeys -l and grep for esc:
    $ dumpkeys -l |grep -i esc 
    0x001b Escape
    0x081b Meta_Escape

  3. Remap the keycode 58 to the Escape key symbol.
    $ (echo `dumpkeys |grep -i keymaps`;  \
    echo keycode 58 = Escape) \
    | loadkeys -

    Thanks to cjwatson who pointed me to prepending the keymaps statement from dumpkeys. The keymaps statement is a shorthand notation defining what key modifiers you are defining with the key. See man keymaps(5) for more info.

To make the new key mapping permanent, you need to put the loadkeys command in a bootup script.

For my Debian Etch system, I put the
(echo `dumpkeys |grep -i keymaps`; echo keycode 58 = Escape) |loadkeys -
command in /etc/rc.local.

StumbleUpon Toolbar


cjwatson said...

This isn't quite correct according to keymaps(5). You also need to use 'dumpkeys | grep ^keymaps' to find out the proper keymaps line to use. For example, here that returns 'keymaps 0-63'. Then you need to say:

(echo keymaps 0-63; echo keycode 58 = Escape) | loadkeys -

If you don't do this, then by default loadkeys will only change the plain (unmodified) binding, and you'll get confusing behaviour in different modified states.

Peter Leung said...

thanks cjwatson. Comment included in blog content.

Chaanakya said...

I'm having some trouble where the CapsLock (i.e. Control) key sticks until I change VTs and back. This does not happen with the regular Control key. Anyone else having this issue/know how to resolve this?


- Chaanakya