Skip to content

Instantly share code, notes, and snippets.

@sahal
Last active March 7, 2024 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sahal/8bdb35eeb208fc2d5d483aac53fe9fdd to your computer and use it in GitHub Desktop.
Save sahal/8bdb35eeb208fc2d5d483aac53fe9fdd to your computer and use it in GitHub Desktop.
Now that Wayland is default in KDE6, I have to find an alternative way to remap keys

Finding a replacement for xmodmap in Wayland

Input Remap Utils at the Arch Wiki

envremap sounds great!

Find Keyboards

You can usually find input devices by name here:

$ ls -l /dev/input/by-id/
total 0
lrwxrwxrwx 1 root root 9 Mar  7 13:55 usb-13ba_Barcode_Reader-event-if01-> ../event8
lrwxrwxrwx 1 root root 9 Mar  7 13:55 usb-13ba_Barcode_Reader-event-kbd-> ../event5
lrwxrwxrwx 1 root root 9 Mar  7 13:55 usb-13ba_Barcode_Reader-if01-event-mouse -> ../event6
lrwxrwxrwx 1 root root 9 Mar  7 13:55 usb-13ba_Barcode_Reader-if01-mouse -> ../mouse1
lrwxrwxrwx 1 root root 9 Mar  7 13:55 usb-PixArt_USB_Optical_Mouse-event-mouse -> ../event4
lrwxrwxrwx 1 root root 9 Mar  7 13:55 usb-PixArt_USB_Optical_Mouse-mouse -> ../mouse0

As you can see the kbd, or keyboard is at /dev/input/event5 based on the above output. But what is it called?! Lets look at /proc/bus/input/devices

$ cat /proc/bus/input/devices | grep -A3 -B5 event5
I: Bus=0003 Vendor=13ba Product=0018 Version=0110
N: Name="Barcode Reader "
P: Phys=usb-0000:00:14.0-1.4/input0
. . .

I have a weird setup, admittedly. My keyboard is labeled "Barcode Reader " since I use a dongle to conver my keyboard's PS/2 output to USB.

Yes, this dongle has this keyboard device named with a trailing space! This isn't as obvious when you look at the recommended output of envremap! (unless you put a |cat -A at the end of the command)

$ sudo evremap list-devices
. . .
$ sudo evremap list-devices
Name: AT Translated Set 2 keyboard
Path: /dev/input/event3
Phys: isa0060/serio0/input0

Name: Barcode Reader
Path: /dev/input/event5
Phys: usb-0000:00:14.0-1.4/input0
. . .

Configure envremap

I copied the systemd unit file from /usr/lib/systemd/system/ to /etc/systemd/system/ and named them with easy to remember filenames.

sudo cp /usr/lib/systemd/system/envremap.service ps2-dell-keyboard\@envremap.service
sudo cp /usr/lib/systemd/system/envremap.service laptop-keyboard\@envremap.service

Create the config files

$ cat << EOF > /etc/laptop-keyboard.toml

device_name = "AT Translated Set 2 keyboard"
#phys = "usb-0000:00:14.0-1/input0"

[[remap]]
input = ["KEY_CAPSLOCK"]
output = ["KEY_ESC"]
EOF

$ cat << EOF > /etc/ps2-dell-keyboard.toml

device_name = "Barcode Reader "
#phys = "usb-0000:00:14.0-1/input0"

[[remap]]
input = ["KEY_CAPSLOCK"]
output = ["KEY_ESC"]
EOF

update the service units to point to the config files

The following command just lists the toml lines in the unit config.

$ find /etc/systemd/system/ -type f -name "*envremap*" -exec echo "{}"
\; -exec grep toml "{}" \;
/etc/systemd/system/ps2-dell-keyboard@envremap.service
ExecStart=bash -c "/usr/bin/evremap remap /etc/ps2-dell-keyboard.toml
-d 0"
/etc/systemd/system/laptop-keyboard@envremap.service
ExecStart=bash -c "/usr/bin/evremap remap /etc/laptop-keyboard.toml -d 0"

enable and start the new services

$ sudo systemctl enable laptop-keyboard\@envremap.service
$ sudo systemctl start laptop-keyboard\@envremap.service
$ sudo systemctl enable ps2-dell-keyboard\@envremap.service
$ sudo systemctl start ps2-dell-keyboard\@envremap.service

that's it

Of course, envremap's touted feature might also be of use to you:

Remap the CAPSLOCK key so that it produces CTRL when held, but ESC if tapped

If that's the case, follow the instructions on the project repo when creating your .toml config file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment