This guide is split up into two sections, one for laptop manufacturers like Dell which have a built-in file for timeout (which can be edited to set up the timeout), and manufacturers like Asus,which need a bit of bash scripting to achieve the same.
On Dell Laptops
You’ve lucked out if you have a Dell machine, setting the timeout is relatively simple on most of their models. Their timeout values are stored in a file; editing the file should be sufficient.
- Navigate to the directory containing the file.
NOTE: If you don’t have such a file (and you’re using Dell), then search around in
/sys/devicesfor a similar folder. If you searched and still can’t find it, follow the instructions in the next section.
- Open the
stop_timeoutfile in a your text editor.
sudo vim stop_timeout
- Write your desired timeout value in seconds with a
sfor “second” after that. (ex.
30sfor 30 seconds) NOTE: For values over 1 minute, place a
mfor “minute” after the value. (ex.
2mfor 2 minutes.)
For all other Laptops (including Asus)
Though a file for setting the timeout doesn’t exist in most laptops, there would a file to display the current brightness level. Our first step is to poke around our
/sys/ directory and find such a file.
In my Asus machine, it is at
/sys/class/leds/asus::kbd_backlight/brightness. Checking the contents of this file should give you the current brightness level of your keyboard.
sudo cat "/sys/class/leds/asus::kbd_backlight/brightness"
This should show a value (0,1,2,3,etc.) depending on the brightness levels of your keyboard. Now, editing this file with a new value should change the brightness.
NOTE: Editing this file requires superuser priviledges; you need to run it through the
/bin/sh -c command like so:
sudo /bin/sh -c "sudo echo 0 >> /sys/class/leds/asus::kbd_backlight/brightness"
This above line should make your backlight dark. This means that our approach is working. The next step is to create a script that automates this procedure everytime your keyboard is idle for
x amount of seconds.
xprintidlethrough your package manager.
sudo apt-get install xprintidle
yay -S xprintidle
- Store the following bash code into a file in your home directory as a file called
kbd_idle. Edit the
xon line 4 with the desired timeout. Edit the
/sys/file too, if it differs.
#!/bin/bash idle=false idleAfter=x #edit this x with the timeout in milliseconds savedState=0 while true; do idleTimeMillis=$(sudo -u vkk env DISPLAY=:0.0 xprintidle) if [[ $idle = false && $idleTimeMillis -gt $idleAfter ]] ; then savedState=$(cat /sys/class/leds/asus::kbd_backlight/brightness) sudo /bin/sh -c "echo 0 >> /sys/class/leds/asus::kbd_backlight/brightness" idle=true echo "Keyboard dimmed." fi if [[ $idle = true && $idleTimeMillis -lt $idleAfter ]] ; then sudo /bin/sh -c "echo $savedState >> /sys/class/leds/asus::kbd_backlight/brightness" idle=false echo "Keyboard brightened." fi sleep 1 done
- Make this file executable.
chmod ugo+x ~/scripts/kbd_idle
- Now, we have to create a
systemctlservice unit that can run this script in the background each time our computer starts. Save the following code in
[Unit] Description=Disables keyboard backlight when inactive. [Service] ExecStart=/home/*user_name*/scripts/kbd_idle [Install] WantedBy=multi-user.target
- Start this system unit and enable it.
sudo systemctl enable --now kbd_idle.service
This is probably a inefficient way to do it actually, but it works. If anyone has a better method, let me know.