Remapping Keys Under Linux

Quick Start

To swap caps lock and control:

# Make the Caps Lock key be a Control key:

xmodmap -e "remove lock = Caps_Lock" xmodmap -e "add control = Caps_Lock"

# Make the Left Control key be a Caps Lock key:


xmodmap -e "remove control = Control_L" xmodmap -e "add lock = Control_L"

Questions Answered Below

  • How do you remap your keyboard to, say, turn the caps lock key into a control key?
  • How about remapping other keys?
  • What are the underlying concepts that man xmodmap fails to explain?

The instructions in this page apply only to Linux in an X environment (like KDE).

Terminology

  • Keycode

    A keycode represents a key. Each key on the keyboard has a unique keycode.

  • Keysym

    A keysym represents an action (I use the terms "action" and "keysym" synonymously below). Examples include "print the letter c " and "start behaving like the left shift key has been pressed".

  • Modifiers

    The modifiers include shift, control, (caps) lock, and others (mod1 through mod5). Modifiers add another level of indirection and are managed with their own set of commands in xmodmap .

How These Relate To One Another

Keycodes, keysyms, and modifiers relate in the following way:

keycodekeysymmodifier (optional)

So for example, on my keyboard:

keycode 38 (the 'a' key)keysym 0x61 (the symbol 'a')

 

keycode 50 (the left 'shift' key)keysym 0xffe1 (the action 'the left shift key is down')the shift modifier

Note that technically, each keycode can be mapped to more than one keysym. The first mapping applies when no modifier is pressed; the second applies when the shift key is pressed. (I haven't figured out how to use the third and fourth yet.) So for example, the second mapping on my 'a' key is:

keycode 38 (the 'a' key)keysym 0x61 (the symbol 'A')

In other words, when modifier 'shift' is active, my 'a' key generates an 'A' instead of an 'a'.

Viwing Your Settings

  • xmodmap -pke displays the mapping between and keysyms. For example (key on the left, action on the right):
    ...
    keycode 37 = Control_L
    keycode 38 = a A agrave agrave acircumflex adiaeresis acircumflex adiaeresis
    ...
    keycode 66 = Caps_Lock
    ...

    This tells me that keycode 37 (which happens to be my left control key) is mapped to the Control_L action. Keycode 38 (my 'a') key is mapped to the action 'a' (with no modifieres pressed), the action 'A' (with shift pressed) or a variety of other actions when I'm pressing other modifiers. The last line says that keycode 66 (which happens to be my Caps Lock key) is mapped to the Caps_Lock action.

  • xmodmap -pm displays the mapping between modifiers and keysyms. For example (modifiers on the left, keysyms on the right):
        shift       Shift_L (0x32),  Shift_R (0x3e)
    lock
    control Caps_Lock (0x42), Control_R (0x6d), Control_L (0x25)
    ...

    The first line says that the "left shift" action and the "right shift" action both invoke the "shift". The second line says that no keysym invokes the "caps lock" modifier. The third says that the Caps_Lock action and the two Control actions invoke the control modifier.

  • xev

    Allows you to view keycodes and keysyms by pressing the key. For example, when I press the left 'shift' key:

        KeyPress event, serial 25, synthetic NO, window 0x3200001,
    root 0xc8, subw 0x0, time 126222719, (-659,738), root:(1087,758),
    state 0x0, keycode 50 (keysym 0xffe1, Shift_L), same_screen YES,
    XLookupString gives 0 bytes: ""

    Line 3 tells me that the left shift key has keycode 50, and that it's currently mapped to keysym 0xffe1 (Shift_L).

Changing Your Settings

Say you want to map the caps lock key to be the control modifier. You have two sensible choices for how to do this:

Caps Lock KeyCaps Lock actionControl Modifier

 

Caps Lock KeyControl_L actionControl Modifier

To do the first, you need to change the action → modifier mapping. Do this as follows:

xmodmap -e "remove lock = Caps_Lock"
xmodmap -e "add control = Caps_Lock"

To do the second, you need to change the keycode → action mapping, so you'll need to know the keycode of your caps lock key. To find the keycode for your caps lock key use xev, as described above. Mine is 66. So:

xmodmap -e "keycode 66 = Control_L"

Help!

If you mess things up, the simplest way to fix things is to log out of the window manager and log back in.

For More Information

  • /usr/include/X11/keysymdef.h

    This file will show you what keysyms exist. Note that you need to omit the XK_ prefix when specifying the keysym.

Notes

  • This is all determined from experimenting, so it may be wrong. But I think it's right!
  • I did this on an x86 under (Red Hat) Linux in KDE, with a basic-looking HP keyboard -- your results may vary.

By David Vespe, April 2006

 

 

参考: 哥伦比亚大学David Vespe

你可能感兴趣的:(linux,manager,action,each,include,keyboard)