The keyboard backlight of my notebook can be toggled via the ‘Fn’-key. But it will stay on until turned off manually. This is quite annoying when watching movies because it dazzles in dark scenes. Another good reason to automatically turn it off after a certain idle time is to save some energy. Therefore i wrote a script using brightnessctl to toggle the manually set brightness value.

#!/bin/sh

FILE="brightness.txt"

if [[ $1 == "on" ]];
then
   brightnessctl -d tpacpi::kbd_backlight set $(cat $FILE)
fi

if [[ $1 == "off" ]];
then
   brightnessctl -d tpacpi::kbd_backlight get > $FILE
   brightnessctl -d tpacpi::kbd_backlight set 0
fi

As I’m using swaywm as window manager, the easiest way to call this script is to call it via swayidle. Below is my swayidle config file.

timeout 20 '. ~/Documents/scripts/toggle_keyboad_backlight.sh off' resume '. ~/Documents/scripts/toggle_keyboad_backlight.sh on'
timeout 300 'swaymsg output * dmps off' resume 'swaymsg output * dpms on'
timeout 600 'swaylock'
before-sleep 'swaylock'