Notes to Self

Alex Sokolsky's Notes on Computers and Programming

Linux Keyboard Input

Architecture and Abstraction Layers

Abstraction layers, from low to high :

Relevant apps:

Swap Ctrl and CapsLock by manipulating the keyboard layout

This:

Therefore this is the easiest way to set things up!

Install the tweak-tool:

sudo apt-get install gnome-tweak-tool

Then: Keyboard & Mouse / Additional Layout Options / Caps Lock behavior, select, e.g., Caps Lock is also a Ctrl.

This does NOT work in:

Swap Ctrl and CapsLock by manipulating the keymap

Linux Keyboard in a Console.

Not tested extensively. I thought I might as well do what is described in the next section.

Swap Ctrl and CapsLock by remapping scancode to keycode

Map scancodes to keycodes, man udev, setkeycodes, showkey.

To identify the scancodes, run sudo showkey -s and press the keys of interest. I learned the following scancodes:

To identify the keycodes, run sudo showkey -k and press the keys of interest. I learned the following keycodes:

From man page: “It is hardwired in the current kernel that in the range 1-88 (0x01-0x58) keycode equals scancode”.

Alternative #1, use setkeycodes, NOT for USB keyboards

NOTE: “USB keyboards have standardized keycodes and setkeycodes does NOT affect them at all.” If still interested, to swap CapsLock with Control:

setkeycodes 3a 29 1d 58

Alternative #2, use udev

This sequence of steps worked for me. If you are not using systemd - you are on your own.

Step 1. Boot into text console

Get the current systemctl target:

systemctl get-default

I’ve got graphical.target. Remember this. We will restore it in step 3.

Now set the default target to multi-user.target:

sudo systemctl set-default multi-user.target

and reboot, e.g. with sudo reboot.

Step 2. Use udev to remap scancode to keycode

Create /etc/udev/hwdb.d/90-custom-keyboard.hwdb:

evdev:atkbd:*
 KEYBOARD_KEY_3a=leftctrl
 KEYBOARD_KEY_1d=capslock

Each line is in the format: KEYBOARD_KEY_<scancode>=<keycode>, where

Manually update the /etc/udev/hwdb.bin to include newly created /etc/udev/hwdb.d/90-custom-keyboard.hwdb:

sudo systemd-hwdb update

Reload the Hardware Database Index using udevadm:

sudo udevadm trigger

To verify:

#sudo udevadm info

By now your CapsLock and Ctrl should be swapped. This worked on my new laptop but not on an old one. Go figure ;-(.

Step 3. Boot back into GUI

sudo systemctl set-default graphical.target
sudo reboot

Verify the GUI retained the remapped keys.