Linux Keyboard Backlight with Keyboard Shortcut on Manjaro Deepin

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.

 

Step One

From the Arch Wiki Reference:
Install upower and python-dbus packages, as this is a Python script.

 

Step Two

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[1] == "--up" or sys.argv[1] == "+":
            if len(sys.argv) == 3:
                print(kb_light_set(int(sys.argv[2])))
            else:
                print(kb_light_set(17))
        elif sys.argv[1] == "--down" or sys.argv[1] == "-":
            if len(sys.argv) == 3:
                print(kb_light_set(-int(sys.argv[2])))
            else:
                print(kb_light_set(-17))
        else:
            print("Unknown argument:", sys.argv[1])
    else:
        print("Script takes one or two argument.", len(sys.argv) - 1, "arguments provided.")

Gearbest Lenovo Legion R720 Gaming Laptop 15.6 inch
Lenovo Legion R720 Gaming Laptop 15.6 inch only $958.99

 

Step Three

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:

KeyboardBright


#!/bin/bash
/usr/local/bin/kb-light.py + 1

KeyboardDim


#!/bin/bash
/usr/local/bin/kb-light.py - 1

 

Step Four

Next up, we just need to assign them to keyboard shortcuts! In Manjaro Deepin, you access that from Control Center, then scroll to Keyboard:


Select “ShortCut” 

Select “Add Custom ShortCut” 

Add a Name, Browse to KeyboardBright or KeyboardDim in \usr\local\bin, and select a Key Combo 


 
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!

Scott Haner

Thanks for reading TechZerker! I’m Scott, a Canadian tech professional for over a decade with a wide range of experience. I created TechZerker as my own source to talk about a variety of tech subjects, from reviews of hardware I get my hands on, challenges I see in my tech work, gaming with focus on nostalgic gaming (games over 10-ish years old, but not Retro), and more recently my explorations in Linux and Linux gaming as a long time Windows Insider and fan. I am passionate about the tech I work and live with and enjoy a good, intelligent discussion on all these topics.