Remapping Keys Under Linux

来源:互联网 发布:用手机秒杀淘宝 编辑:程序博客网 时间:2024/06/06 21:45

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 keyinto a control key?
  • How about remapping other keys?
  • What are the underlying concepts that man xmodmapfails to explain?

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

Terminology

  • Keycode

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

  • Keysym

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

  • Modifiers

    The modifiers include shift, control, (caps) lock, and others (mod1through mod5). Modifiers add another level of indirection and aremanaged 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 onekeysym. The first mapping applies when no modifier is pressed; thesecond applies when the shift key is pressed. (I haven't figured outhow to use the third and fourth yet.) So for example, the secondmapping 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' keygenerates an 'A' instead of an 'a'.

Viwing Your Settings

  • xmodmap -pke displays the mapping between andkeysyms. 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 controlkey) is mapped to the Control_L action. Keycode 38 (my 'a') key ismapped to the action 'a' (with no modifieres pressed), the action 'A'(with shift pressed) or a variety of other actions when I'm pressingother modifiers. The last line says that keycode 66 (which happens tobe my Caps Lock key) is mapped to the Caps_Lock action.

  • xmodmap -pm displays the mapping between modifiersand keysyms. For example (modifiers on the left, keysyms on theright):
        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 "rightshift" action both invoke the "shift". The second line says that nokeysym invokes the "caps lock" modifier. The third says that theCaps_Lock action and the two Control actions invoke the controlmodifier.

  • xev

    Allows you to view keycodes and keysyms by pressing the key. Forexample, 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 thatit'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 → actionmapping, so you'll need to know the keycode of your caps lock key. Tofind the keycode for your caps lock key use xev, asdescribed 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 outof 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 toomit 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 abasic-looking HP keyboard -- your results may vary.

By David Vespe, April 2006

 

 

参考: 哥伦比亚大学David Vespe

原创粉丝点击