Running Linux on a laptop with Backlit Keyboard? Connecting a cool Backlit gaming keyboard to your Linux desktop? No backlight or ability to control the backlight?
Today I’ll quickly build on the details on the Arch Wiki for keyboard backlight to help you get that working like I did on my ASUS Republic of Gamers laptop!
First off, as with any Arch Linux based distro, your number one reference source should be the venerable Arch Wiki! In this how-to, I’m using the Arch based Manjaro Deepin, but the core of these instructions will work similar on any Arch distro, and likely on other Linux distro’s as well with minor adjustments.
To complete these steps, as expected you’ll need your Root or Sudo access, primarily for saving the necessary files via your File Manager in \usr\local\bin. In Manjaro Deepin, you can just navigate to that directory, then right click and open as Admin, with other file managers if all else fails you can Sudo FileManagerName.
Create the File: /usr/local/bin/kb-light.py
And place the following script in it:
#!/usr/bin/env python3 import dbus import sys def kb_light_set(delta): bus = dbus.SystemBus() kbd_backlight_proxy = bus.get_object('org.freedesktop.UPower', '/org/freedesktop/UPower/KbdBacklight') kbd_backlight = dbus.Interface(kbd_backlight_proxy, 'org.freedesktop.UPower.KbdBacklight') current = kbd_backlight.GetBrightness() maximum = kbd_backlight.GetMaxBrightness() new = max(0, min(current + delta, maximum)) if 0 <= new <= maximum: current = new kbd_backlight.SetBrightness(current) # Return current backlight level percentage return 100 * current / maximum if __name__ == '__main__': if len(sys.argv) == 2 or len(sys.argv) == 3: if sys.argv == "--up" or sys.argv == "+": if len(sys.argv) == 3: print(kb_light_set(int(sys.argv))) else: print(kb_light_set(17)) elif sys.argv == "--down" or sys.argv == "-": if len(sys.argv) == 3: print(kb_light_set(-int(sys.argv))) else: print(kb_light_set(-17)) else: print("Unknown argument:", sys.argv) else: print("Script takes one or two argument.", len(sys.argv) - 1, "arguments provided.")
Lenovo Legion R720 Gaming Laptop 15.6 inch only $958.99
For Manjaro Deepin, as we’ll see further down, the Setting Manager for keyboard shortcuts won’t let you enter commands to pass a script, only select a file. The commands to bind to a key for this script are simply:
/usr/local/bin/kb-light.py + 1 and
/usr/local/bin/kb-light.py - 1 to increase and decrease brightness. But because I can’t enter that directly in the Setting Manager, create these two files (no file extension) in /usr/local/bin
Each of these are very simple files with the following contents:
#!/bin/bash /usr/local/bin/kb-light.py + 1
#!/bin/bash /usr/local/bin/kb-light.py - 1
Next up, we just need to assign them to keyboard shortcuts! In Manjaro Deepin, you access that from Control Center, then scroll to Keyboard:
In my case, I used Ctrl + F3 and Ctrl + F4, because my ASUS Keyboard originally used with Icons on the keys FN + F3/F4, except Linux isn’t reading that FN key.
Once those shortcuts are created, you should be good to go!
In my case no reboot or service restart of any kind was needed, I was immediatly able to use those shortcut keys to go from the Linux Default on this system of no Backlight, through three levels of brightness (33.3%, 66.6%, and 100%). Different keyboard backlights may treat + 1 and – 1 in the script differently. If you find it takes you 8 – 10 shortcut presses to get up and down between your preferred Dim/Bright settings, you can easily update those two scripts to +/- 2 or 3.
Do you have any other methods to complete this function?
Any other handy day-to-day scripts to get the best from your hardware?
Let me know in the comments or on Twitter!