Effectively swapping Caps Lock and Control on Lenovo X1 Carbon 5th gen

It’s easy enough to swap ctrl and caps lock in Xorg with setxkbmap -option "ctrl:swapcaps", but it gets irritating when working on the console.

Roughly the process is:

  1. Find the event codes generated by the caps lock key
  2. Find the laptop’s correct hardware database (hwdb) description
  3. Map the keycode found above to the hwdb description found above and create a hwdb rule

From evtest:

  ...
  Event: time 1495942112.747149, type 4 (EV_MSC), code 4 (MSC_SCAN), value 3a
  Event: time 1495942112.747149, type 1 (EV_KEY), code 58 (KEY_CAPSLOCK), value 1
  Event: time 1495942112.747149, -------------- SYN_REPORT ------------
  Event: time 1495942112.841422, type 4 (EV_MSC), code 4 (MSC_SCAN), value 3a
  Event: time 1495942112.841422, type 1 (EV_KEY), code 58 (KEY_CAPSLOCK), value 0
  Event: time 1495942112.841422, -------------- SYN_REPORT ------------
  ...

So we know that the event code associated with the caps lock key is 3a

Then via ememu-describe:

evdev:atkbd:dmi:bvnLENOVO:bvrN1MET31W(1.16):bd03/10/2017:svnLENOVO:pn20HRCTO1WW:pvrThinkPadX1Carbon5th:rvnLENOVO:rn20HRCTO1WW:rvrSDK0J40697WIN:cvnLENOVO:ct10:cvrNone:

We don’t need all of that; honestly something just some wildcard with the X1C5’s model number is fine, so finally from /etc/udev/hwdb.d/10-thinkpad-keyboard.hwdb:

evdev:*pn20HRCTO1WW*
  KEYBOARD_KEY_3a=leftctrl
[root@milo hwdb.d]# pwd
/etc/udev/hwdb.d