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)...
    0x9c


    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)...
    0x9c
    0x3a
    0xba


    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.

4 comments:

Anonymous 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?

Thanks!

- Chaanakya

remapper said...

On Debian, the procedure described in above post did not work for making CAPS into CTRL. Then I found useful info in

man keyboard

In short, one can achieve CAPS=>CTRL this way:

In /etc/default/keyboard change line XKBOPTIONS= so that it contains ctrl:nocaps , mine is

XKBOPTIONS=compose:menu,ctrl:nocaps

To activate new CAPS behaviour in console, run

setupcon

To activate in X, run

udevadm trigger --subsystem-match=input --action=change