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"
man xmodmap
fails to explain? The instructions in this page apply only to Linux in an X environment (like KDE).
A keycode represents a key. Each key on the keyboard has a unique keycode.
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".
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
.
Keycodes, keysyms, and modifiers relate in the following way:
keycode → keysym → modifier (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'.
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).
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 Key → Caps Lock action → Control Modifier
Caps Lock Key → Control_L action → Control 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"
If you mess things up, the simplest way to fix things is to log out of the window manager and log back in.
/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.
By David Vespe, April 2006
参考: 哥伦比亚大学David Vespe