Keyboard backlight

From ArchWiki
Revision as of 10:17, 13 December 2016 by Kusakata (talk | contribs) (add ja link)
Jump to: navigation, search

Any vendor

You can control your computer keyboard backlight via the D-Bus interface. The benefits of using it are that no modification to device files is required and it is vendor agnostic.

Here is an example implementation in Python 3. Place the following script in /usr/local/bin/ and make it executable. You can then map your keyboard shortcuts to run /usr/local/bin/ + and /usr/local/bin/ - to increase and decrease your keyboard backlight level.

#!/usr/bin/env python3
# coding: utf-8

from sys import argv
import dbus

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, current + delta)

    if new >= 0 and new <= maximum:
        current = new

    # Return current backlight level percentage
    return 100 * current / maximum

if __name__ == '__main__':
    if len(argv[1:]) == 1:
        if argv[1] == "--up" or argv[1] == "+":
            # ./ (+|--up) to increment
        elif argv[1] == "--down" or argv[1] == "-":
            # ./ (-|--down) to decrement
            print("Unknown argument:", argv[1])
        print("Script takes exactly one argument.", len(argv[1:]), "arguments provided.")


Warning: The following way is not recommended. It provides world-writeable permissions to the keyboard backlight device file meaning that any and every user can control it.

The keyboard backlight file is usually locked out from editing. To unlock this file at bootup, you will need to create a systemd service.

Description=Asus Keyboard Backlight

ExecStart=/bin/chmod 666 /sys/class/leds/asus::kbd_backlight/brightness


You are now able to use a keyboard backlight changer script. For an example, see ASUS G55VW#keyboard backlight script.