{
"name":Himalay Sunuwar,
"bio":I'm a programmer from Himalayas, I listen to Metal and eat Rice. No I don't smoke, drink, or do drugs.,

Automate NumLock toggle on external keyboard in Linux

Himalay Sunuwar
Himalay Sunuwar
May 28th, 2020 · 1 min read

Udev rules are placed in /etc/udev/rules.d/ folder with .rules extension. The udev rules are matched against the device attributes when Udev receives device events. In this post, we are going to automate the toggle of NumLock when an external keyboard is connected or removed using two lines of udev rules. We are going to use numlockx command when our defined Udev rule is matched.

Identifying the device and its attributes

First of all, we must identify our device (in our case an external keyboard) by simply running below command in the terminal.

1ls /dev/input/by-id/

In my case, my external keyboard is usb-USB_USB_Keyboard-event-kbd. Yours can be named differently, you can run the above command before and after plugging in the keyboard to identify it. Once we’ve our keyboard’s device id, next we find out the device attributes to use in Udev rules by running below command.

1udevadm info --attribute-walk /dev/input/by-id/keyboard_device_id

Replace keyboard_device_id with your keyboard’s device id.

udevadm info of an external keyboard
udevadm info of an external keyboard

Identifying the user’s Authorization file path and Display

Since we are going to use numlockx which uses the X11 session, we must set XAUTHORITY and DISPLAY environment variables for udev system. In order to find out the path of authorization file, we run the below command. If the output is empty, then it is /home/username/.Xauthority (Replace the username with your username).

1echo $XAUTHORITY

The following commands prints out the value for DISPLAY.

1echo $DISPLAY
XAUTHORITY and DISPLAY values
XAUTHORITY and DISPLAY values

Adding the Udev rules

Finally we are going to add a Udev rules file with rules to toggle NumLock based on device events and attributes. Make sure to replace authorization_file_path, display_value, and username values with your own, then save the rules in /etc/udev/rules.d/usb-keyboard.rules file.

1# Turn on NumLock when a usb keyboard is plugged in.
2ACTION=="add", SUBSYSTEM=="usb", ENV{XAUTHORITY}="authorization_file_path", ENV{DISPLAY}="display_value", ATTRS{product}=="USB Keyboard", RUN+="/usr/bin/su username -c '[ -f $XAUTHORITY ] && /usr/bin/numlockx on'"
3
4# Turn off NumLock when a usb keyboard is unplugged.
5ACTION=="remove", SUBSYSTEM=="usb", ENV{XAUTHORITY}="authorization_file_path", ENV{DISPLAY}="display_value", ATTRS{product}=="USB Keyboard", RUN+="/usr/bin/su username -c '[ -f $XAUTHORITY ] && /usr/bin/numlockx off'"

Run the following commands to reload Udev rules without rebooting your machine.

1sudo su
2udevadm control --reload-rules
3udevadm trigger

Toggle NumLock in startup

Adding this line of code to your ~/.bashrc or ~/.zshrc file will automatically toggle NumLock if you attached or detached a keyboard while your machine was off or in-sleep.

1[[ `lsusb | grep -i keyboard` != "" ]] && numlockx on || numlockx off
0

More articles

Keeping code snippets handy in Chrome DevTool

We can save any piece of JavaScript as Snippet in Chrome DevTool and we can run it on DevTool console any time we need.

November 8th, 2017 · 1 min read

Best MoMo place in Kathmandu

Everyone loves Mo:Mos in Kathmandu. It's a love that dares speak its name over and over again.

January 20th, 2017 · 1 min read
"social":
{
"github":@himalay,
"linkedin":@sunuwar,
"twitter":@HimalaySunuwar,
"instagram":@HimalaySunuwar,
},
"sitemap":/sitemap.xml,
"rss":/rss.xml
}