how to swap ctrl and capslock using xmodmap?

19,910

Solution 1

From Remap Caps Lock:

man xmodmap shows how to swap the left control key and the CapsLock key:

!
! Swap Caps_Lock and Control_L
!
remove Lock = Caps_Lock
remove Control = Control_L
remove Lock = Control_L
remove Control = Caps_Lock
keysym Control_L = Caps_Lock
keysym Caps_Lock = Control_L
add Lock = Caps_Lock
add Control = Control_L

Those keysym lines are important since they're the ones that are mapping the keycodes to the opposing keys, i.e. keycode for Capslock goes to Control L and vice versa.

excerpt from the xmodmap man page*

   keysym KEYSYMNAME = KEYSYMNAME ...
           The KEYSYMNAME on the left hand side is translated into matching 
           keycodes used to perform the corresponding set of keycode 
           expressions.  Note that if the same keysym is bound to multiple 
           keys, the expression is executed for each matching keycode.

Seeing the effect

You can use the tool xev to see that the keys have been literally remapped. So Capslock now sends the scancode for Control L.

Example

Pressing Capslock sends Control L.

$ xev
KeyPress event, serial 36, synthetic NO, window 0x3e00001,
    root 0x86, subw 0x0, time 890946390, (803,237), root:(804,294),
    state 0x2, keycode 66 (keysym 0xffe3, Control_L), same_screen YES,
    XLookupString gives 0 bytes: 
    XmbLookupString gives 0 bytes: 
    XFilterEvent returns: False

KeyRelease event, serial 36, synthetic NO, window 0x3e00001,
    root 0x86, subw 0x0, time 890946462, (803,237), root:(804,294),
    state 0x6, keycode 66 (keysym 0xffe3, Control_L), same_screen YES,
    XLookupString gives 0 bytes: 
    XFilterEvent returns: False

Pressing Control L sends Capslock.

$ xev
KeyPress event, serial 36, synthetic NO, window 0x3e00001,
    root 0x86, subw 0x0, time 891083183, (793,9), root:(794,66),
    state 0x0, keycode 37 (keysym 0xffe5, Caps_Lock), same_screen YES,
    XLookupString gives 0 bytes: 
    XmbLookupString gives 0 bytes: 
    XFilterEvent returns: False

KeyRelease event, serial 36, synthetic NO, window 0x3e00001,
    root 0x86, subw 0x0, time 891083302, (793,9), root:(794,66),
    state 0x2, keycode 37 (keysym 0xffe5, Caps_Lock), same_screen YES,
    XLookupString gives 0 bytes: 
    XFilterEvent returns: False

References

Solution 2

Correct. The "remove" and "add" lines disable / enable the keys. The commands that actually do the switch are the keysym commands. So it's never going to work without the keysym commands.

Share:
19,910

Related videos on Youtube

SparedWhisle
Author by

SparedWhisle

Updated on September 18, 2022

Comments

  • SparedWhisle
    SparedWhisle over 1 year

    I know this xmodmap script can swap ctrl and capslock:

    remove Lock = Caps_Lock
    remove Control = Control_L
    keysym Caps_Lock = Control_L
    keysym Control_L = Caps_Lock
    add Lock = Caps_Lock
    add Control = Control_L
    

    I don't quite understand it. So I tried this:

    remove Lock = Caps_Lock
    remove Control = Control_L
    add Lock = Control_L
    add Control = Caps_Lock
    

    And this script doesn't work.
    Could some explain this(why the 1st script works and the other one doesn't) in simple words?