Determining List Key Events with PS/2 Keyboard: Setting Keycodes using Systemd Service
In this article, we will explore how to set custom keycodes for your PS/2 keyboard using a Systemd service. This process is particularly useful for older keyboards that emit scancodes instead of tied keycodes.
Prerequisites
Before we begin, ensure that you have the following:
- A Linux system with Systemd
- Access to the terminal
- A PS/2 keyboard
Key Concepts
This article assumes a basic understanding of the following concepts:
- Linux
- Systemd
- PS/2 keyboards
- Keycodes
Determining Keycodes
Before we can set custom keycodes, we need to determine the scancodes for the keys on our PS/2 keyboard. We can use the evtest utility to accomplish this.
Installing evtest
If you don't have evtest installed, you can install it using your package manager. For example, on Debian-based systems:
sudo apt-get install linux-tools-common linux-headers-$(uname -r)Determining Scancodes
Once you have evtest installed, connect your PS/2 keyboard to your system and run the following command:
sudo evtest /dev/input/event*This command will display a list of all input devices connected to your system. Look for your keyboard and note the device number. Then, use the following command to display the scancodes for each key press:
sudo evtest /dev/input/eventXReplace X with the number of your keyboard device. Press each key on your keyboard and note the corresponding scancode.
Setting Keycodes using Systemd
Now that we have determined the scancodes for our keys, we can set custom keycodes using a Systemd service. Create a new file in /etc/systemd/system called mykeyboard.service with the following content:
mykeyboard.service
[Unit]
Description=My custom keyboard
After=sysinit.target
[Service]
Type=oneshot
ExecStart=/usr/bin/evtest --grab /dev/input/eventX --key X.Y --type key --time 50 --repeat 10 --mode keydown
Restart=always
[Install]
WantedBy=multi-user.target
Replace X with the number of your keyboard device and Y with the scancode for the key you want to set. For example, if the scancode for the "F1" key is 0x45, the content of the file would look like:
[Unit]
Description=My custom keyboard
After=sysinit.target
[Service]
Type=oneshot
ExecStart=/usr/bin/evtest --grab /dev/input/eventX --key 0x45 --type key --time 50 --repeat 10 --mode keydown
Restart=always
[Install]
WantedBy=multi-user.target
Save and close the file. Then, reload the Systemd configuration:
sudo systemctl daemon-reloadEnabling the Service
Now that we have created the service, we can enable it to start automatically at boot:
sudo systemctl enable mykeyboard.serviceIn this article, we learned how to determine scancodes for keys on a PS/2 keyboard using the evtest utility and set custom keycodes using a Systemd service.