Warning
This article was last updated on 2022-04-10, the content may be out of date.
I once had struggle trying to get my backlight work in my Arch system with i3wm.
So I thought I would share.
- Basic usage is as simple as
1
2
3
|
kbdbacklight up
kbdbacklight down
kbdbacklight [ 0 | 1 | 2 | 3 ]
|
1
2
3
|
# increase/decrease keyboard brightness
bindsym XF86KbdBrightnessUp exec kbdbacklight up
bindsym XF86KbdBrightnessDown exec kbdbacklight down
|
The scripts is available in the end of this post. Just save somewhere in your PATH
and enjoy. I went with the name kbdbacklight
and used it in the examples and i3 config above, please be mindful to update those if you name it differently.
Most Linux distributions encourage a practice in which each user has a specific directory for the programs he/she personally uses. This directory is called bin and is a subdirectory of your home directory. If you do not already have one, create it with the following command:
1
|
[me@linuxbox me]$ mkdir bin
|
Move your script into your new bin
directory:
Add directories to your path with the following command, where directory is the name of the directory you want to add:
1
|
[me@linuxbox me]$ export PATH=$PATH:directory
|
In this case, it is:
1
|
[me@linuxbox me]$ export PATH=$PATH:~/bin
|
Now, you are all set.
And here is the backlight script (click to expand):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
#!/bin/sh
# By Giorgos Keramidas
# backlight_get
# Print current keyboard brightness from UPower to stdout.
backlight_get()
{
dbus-send --type=method_call --print-reply=literal --system \
--dest='org.freedesktop.UPower' \
'/org/freedesktop/UPower/KbdBacklight' \
'org.freedesktop.UPower.KbdBacklight.GetBrightness' \
| awk '{print $2}'
}
# backlight_get_max
# Print the maximum keyboard brightness from UPower to stdout.
backlight_get_max()
{
dbus-send --type=method_call --print-reply=literal --system \
--dest='org.freedesktop.UPower' \
'/org/freedesktop/UPower/KbdBacklight' \
'org.freedesktop.UPower.KbdBacklight.GetMaxBrightness' \
| awk '{print $2}'
}
# backlight_set NUMBER
# Set the current backlight brighness to NUMBER, through UPower
backlight_set()
{
value="$1"
if test -z "${value}" ; then
echo "Invalid backlight value ${value}"
fi
dbus-send --type=method_call --print-reply=literal --system \
--dest='org.freedesktop.UPower' \
'/org/freedesktop/UPower/KbdBacklight' \
'org.freedesktop.UPower.KbdBacklight.SetBrightness' \
"int32:${value}}"
}
# backlight_change [ UP | DOWN | NUMBER ]
# Change the current backlight value upwards or downwards, or
# set it to a specific numeric value.
backlight_change()
{
change="$1"
if test -z "${change}" ; then
echo "Invalid backlight change ${change}." \
"Should be 'up' or 'down'." >&2
return 1
fi
case ${change} in
[1234567890]|[[1234567890][[1234567890])
current=$( backlight_get )
max=$( backlight_get_max )
value=$( expr ${change} + 0 )
if test ${value} -lt 0 || test ${value} -gt ${max} ; then
echo "Invalid backlight value ${value}." \
"Should be a number between 0 .. ${max}" >&2
return 1
else
backlight_set "${value}"
notify-send -t 800 "Keyboard brightness set to ${value}"
fi
;;
[uU][pP])
current=$( backlight_get )
max=$( backlight_get_max )
if test "${current}" -lt "${max}" ; then
value=$(( ${current} + 1 ))
backlight_set "${value}"
notify-send -t 800 "Keyboard brightness set to ${value}"
fi
;;
[dD][oO][wW][nN])
current=$( backlight_get )
if test "${current}" -gt 0 ; then
value=$(( ${current} - 1 ))
backlight_set "${value}"
notify-send -t 800 "Keyboard brightness set to ${value}"
fi
;;
*)
echo "Invalid backlight change ${change}." >&2
echo "Should be 'up' or 'down' or a number between" \
"1 .. $( backlight_get_max )" >&2
return 1
;;
esac
}
if test $# -eq 0 ; then
current_brightness=$( backlight_get )
notify-send -t 800 "Keyboard brightness is ${current_brightness}"
else
# Handle multiple backlight changes, e.g.:
# backlight.sh up up down down up
for change in "$@" ; do
backlight_change "${change}"
done
fi
|