Skip to main content

Exchange 'Caps_Lock' and 'Control_L' (Left Control) using xmodmap

The man page of xmodmap is showing us a way of exchanging/swaping Caps_Lock and Control_L:

! file: ~/.xmodmap

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

How to run the script:

  1. Save the script in ~/.xmodmap.
  2. Run xmodmap ~/.xmodmap
  3. Check your Caps Lock and Left Control to see the effect.

However, the script will really swap it. Because if you rerun the script again, it'll swap it back.


The right way to do it, is to enforce the keyboard key Caps Lock to act as a Left Control key and vice versa.

When I say the keyboard key, I mean the hard touchable key on the keyboard. Pressing a key on the keyboard will send a keycode to the computer, which is how keyboard devices work.

You need to understand that Swaping any key does not swap the hardware. It just changes the reaction of your operating system when it gets a specific keycode.

The keyboard Caps Lock key sends keycode 66, while the keyboard Left Control key sends 37.

So, we just need to assign keycode 37 to the action of Caps Lock and keycode 66 to the action of Left Control.

Therefore, here's the script that'll work no matter how many times you rerun the script.

! file: ~/.xmodmap

!
! Swap Caps_Lock and Control_L
!
remove Lock = Caps_Lock
remove Lock = Control_L
remove Control = Caps_Lock
remove Control = Control_L
keycode 37 = Caps_Lock
keycode 66 = Control_L
add Lock = Caps_Lock
add Control = Control_L

Some useful tools here that might help you with other key exchanges:

xev is a X event tester, and it'll help you tracking any key presses in the logging output. For example, pressing keyboard key a gives the following report:

KeyPress event, serial 37, synthetic NO, window 0x5600001,
    root 0x286, subw 0x0, time 40063331, (277,-166), root:(332,696),
    state 0x0, keycode 38 (keysym 0x61, a), same_screen YES,
    XLookupString gives 1 bytes: (61) "a"
    XmbLookupString gives 1 bytes: (61) "a"
    XFilterEvent returns: False

KeyRelease event, serial 37, synthetic NO, window 0x5600001,
    root 0x286, subw 0x0, time 40063412, (277,-166), root:(332,696),
    state 0x0, keycode 38 (keysym 0x61, a), same_screen YES,
    XLookupString gives 1 bytes: (61) "a"
    XFilterEvent returns: False

It shows that keyboard key a is sending us the keycode 38.

Comments

Comments powered by Disqus