Automating your keyboard’s backlit with Bash
You have a keyboard, it backlits at night, but not in day. You save a bit of energy, and your keyboard looks cool at night. Great!
But. wait a minute.
I’m learning Bash lately, and I guess sharing my learning experience with you will be fruitful on my writing experience as well. Bear with me.
Going back.
So I used a simple algorithm in making my keyboard a little bit smarter with Bash. Bash’s syntax is a bit strange for me, and took me a day before getting used to it. But why Bash when I can do it with a language I’m comfortable with, like Python? Well, exposing yourself to new habits from time to time exercises your brain’s neuroplasticity, which is good. So here’s how the backlit’s simple algorithm works, Living near the equator gives us full 12 hours of daylight/darkness everyday.
Therefore I shall conclude, that my keyboard must be cool in between 6pm and 6am, and eco-friendly between 6am and 6pm. Thus, giving us this logic flow:
Time of day | Switch backlit to | When to switch backlit again? |
---|---|---|
6am to 5:59pm | OFF | Next 6 hours |
6pm to 5:59am | ON | Next 6 hours |
Okay then, so how do we compute the next 6 hours? We need to use military time, in order to simplify things. Calculating up to 24th hour is easier to do. But since time is presented in 12hr format in my computer, I adjusted my formulas.
Time of day | Next backlit switch | Formula to next backlit switch (where h is the time of day) |
---|---|---|
6am to 5:59pm | 6pm | 18 — h |
6pm to 11:59pm | 6am | (12 — h ) + 6 |
12am to 6am | 6am | 6 — h |
But wait, why those formulas? Because 6pm is the 18th hour of day. The second formula is the same as the first, I just put it into the context of time resetting back to 0 at 12am. I guess, for the sake of a clearer view of the algorithm. Okay, so now the keyboard knows when it should switch. Great. But wait. How should it switch. the scroll lock? Well, for some keyboards, pressing the Scroll Lock key triggers backlit. And for most Linux machines, you can control your keyboard LEDs using xset command. From xset man page:
led The led option controls the keyboard LEDs. This controls the turning on or off of one or all of the LEDs. It accepts an optional integer, a preceding dash(-) or an ‘on/off’ flag. . For example, to turn on the Scroll Lock LED:
xset led named «Scroll Lock»
while true; do current_hr=$((10#$(date +'%H'))) current_min=$((10#$(date +'%M'))) six_am=6 six_pm=18 # sunrise if [[ current_hr -eq six_am ]]; then next_check=43200 # 12 hrs in s xset -led named 'Scroll Lock' echo 'Next check at' $(date -d "now + $next_check seconds")',' 'approx' $(($next_check/60/60)) 'hrs from now.' sleep $next_check # sunset elif [[ current_hr -eq six_pm ]]; then next_check=43200 # 12 hrs in s xset led named 'Scroll Lock' echo 'Next check at' $(date -d "now + $next_check seconds")',' 'approx' $(($next_check/60/60)) 'hrs from now.' sleep $next_check # midnight till before sunrise elif [[ current_hr -lt six_am ]]; then min_to_six=$(($((10#$six_am*60 + 10#0)) - $((10#$current_hr*60 + 10#$current_min)))) next_check=$(($min_to_six * 60)) xset led named 'Scroll Lock' echo 'Next check at' $(date -d "now + $next_check seconds")',' 'approx' $(($next_check/60/60)) 'hrs from now.' sleep $next_check # after sunrise till sunset elif [[ current_hr -lt six_pm ]]; then min_to_six=$(($((10#$six_pm*60 + 10#0)) - $((10#$current_hr*60 + 10#$current_min)))) next_check=$(($min_to_six * 60)) xset -led named 'Scroll Lock' echo 'Next check at' $(date -d "now + $next_check seconds")',' 'approx' $(($next_check/60/60)) 'hrs from now.' sleep $next_check # after sunset till before sunrise elif [[ current_hr -gt six_pm ]]; then hrs_to_twelve_am=$(($((10#24*60 + 10#0)) - $((10#$current_hr*60 + 10#$current_min)))) min_to_six=$(($((10#$hrs_to_twelve_am + 10#0)) + $((10#6*60 + 10#0)))) next_check=$(($min_to_six * 60)) xset led named 'Scroll Lock' echo 'Next check at' $(date -d "now + $next_check seconds")',' 'approx' $(($next_check/60/60)) 'hrs from now.' sleep $next_check fi done
Keyboard backlight
There are various methods to control the keyboard backlight brightness level.
Any vendor
There are a variety ways to manage the brightness level and different helpers tools to accomplish this, such as brightnessctl or light .
The sys pseudo-file system exposes an interface to the keyboard backlight. The current brightness level can be get by reading /sys/class/leds/tpacpi::kbd_backlight/brightness . For example to get the maximum brightness level:
$ cat /sys/class/leds/tpacpi::kbd_backlight/max_brightness
To set the brightness to 1:
# echo 1 > /sys/class/leds/tpacpi::kbd_backlight/brightness
When using brightnessctl you can get a list of available brightness controls with brightnessctl —list , then to show the kbd backlight information:
$ brightnessctl --device='tpacpi::kbd_backlight' info
This will show the absolute and relative current value and the maximum absolute value. To set a different value:
$ brightnessctl --device='tpacpi::kbd_backlight' set 1
xset
Some keyboard manufactores are not recognized by brightnessctl or light , but you can use xorg-xset to control its lights if you are running Xorg.
The first parameter led turns on the led, and -led turns it off, the NUMBER parameters accepts integers for 1 to 32 (each number corresponds to a led in you system, keyboards seem to generally be number 3), or ‘on’ and ‘off’ (on will turn ALL lights on, and off will turn ALL lights off).
D-Bus
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.
The following is an example implementation in Python 3. Install upower and dbus-python packages then place the following script in /usr/local/bin/ and make it executable. You can then map your keyboard shortcuts to run /usr/local/bin/kb-light.py + x and /usr/local/bin/kb-light.py — x to increase and decrease your keyboard backlight level by x amounts.
Tip: You should try with an x = 1 to determine the limits of the keyboard backlight levels
#!/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 0Alternatively the following bash one-liner will set the backlight to the value specified in the argument:
On GNOME
The following can be run from a terminal or mapped to keybindings
$ gdbus call --session --dest org.gnome.SettingsDaemon.Power --object-path /org/gnome/SettingsDaemon/Power --method org.gnome.SettingsDaemon.Power.Keyboard.StepUp $ gdbus call --session --dest org.gnome.SettingsDaemon.Power --object-path /org/gnome/SettingsDaemon/Power --method org.gnome.SettingsDaemon.Power.Keyboard.StepDownOn MATE
This article or section needs language, wiki syntax or style improvements. See Help:Style for reference.
In case you use MATE environment you might get tired with repeated lighting keyboard backlight while logging in, unlocking screen or waking up dimmed display. Following setup prevent from automatic lighting up during any action. The only triggers remain plugging in the adapter and fresh boot. After that you can control keyboard backlight only via hotkeys (eg. ThinkPad Fn + spacebar).
To prevent automatic lighting up just edit file /usr/share/dbus-1/system.d/org.freedesktop.UPower.conf as follows (two occurrences of "deny"):
/usr/share/dbus-1/system.d/org.freedesktop.UPower.confHow to light up back-lit keyboard?
I recently got a backlit keyboard, and I LOVE to write late at night. But I cannot for the life of me figure out how to light it up. It lights up when first plugged in, but nothing happens after that. It is an 'XtremeIT' keyboard. There is a video of someone on Ubuntu who managed to activate it.
What model is the keyboard? I have the old G15 and mine lights up fine. Usually the keyboard itself has a button to enable/dim/brighten the light.
Does the keyboard have problem only with Ubuntu machine and works fine with other platform? Sorry if am sounding foolish.
11 Answers 11
Did you try the script the YouTube poster suggested in his own comments?
Basically use xset to toggle the state of the led backlight.
#!/bin/bash if [ -f /tmp/keyboard_light ]; then xset -led 3 && rm /tmp/keyboard_light else xset led 3 && touch /tmp/keyboard_light fi
It does work! but it turns on the mouse keys, anyway to enable it without losing the numberpad function and having to manually disable it everytime?
My makeshift fix is this command: gsettings set org.gnome.desktop.a11y.keyboard mousekeys-enable true
to turn on backlight, and type:
Just try to type in terminal : Turn on :
it works for Cool Master Keyboard
On my Thinkpad T470S with Ubuntu 20.04 -- Keyboard back-light is enabled out of the box. Pressing [Fn-Space] will toggle between keyboard back-light settings on this laptop. Maybe the manual for your keyboard will provide insight into the key combination that might work for its back-light?
echo 0 | sudo tee /sys/devices/platform/thinkpad_acpi/leds/tpacpi::kbd_backlight/brightness echo 3 | sudo tee /sys/devices/platform/thinkpad_acpi/leds/tpacpi::kbd_backlight/brightness This is the way I switch it on/off askubuntu.com/questions/644410/…
for those who land here because they also want the keyboard to light up BEFORE the login screen:
Finally Found an answer, at least for Ubuntu 14.04
as for how to get the keyboard to light up before the login screen:
/usr/share/lightdm/lightdm.conf.d/50-unity-greeter.conf sudo gedit /usr/share/lightdm/lightdm.conf.d/50-unity-greeter.conf
greeter-setup-script= xset led 3
Is there a way to set the brightness level of the keyboard? As opposed to simply turning it on at the lowest setting?
For my Mi laptop keyboard, above solutions did not work.
I just had to use that F10 key with the appropriate symbol.
The symbol looks like a bold "dash", with little thin dashes going in every direction and representing light.
Found the answer after a long night up with lots of half baked solutions.
# backup your symbols file sudo cp /usr/share/X11/xkb/symbols/usYou may have to do the same in your other layouts if you switch between languages
Also, there is a cache where xkb layouts live. You should clear it before restarting your X server to check the new keyboard symbol file(s).
For a long time, I did this with
xmodmap -e 'add mod3 = Scroll_Lock'
This caused a problem when I started using i3wm. When the backlight was on, the metakey was non-functional. I could turn it on and off with scroll lock and use meta when it was off, but i3 needs that too much and it is too hard to see my keyboard without the light, so this was not an ideal way to do it. The above solution, xset led on is a much better solution. By leaving the keymap alone, I can use meta anytime I need it and always see the keyboard.
I just bought an EagleTec mechanical keyboard with blue backlight, and like J. Chomel, found that I just needed to use a key combination to turn the backlight on or off, enable/disable "breathing" mode, or adjust the brightness. In my case, I'm using it on LinuxMint 17, but it should work on other distributions also.
Here are the backlight functions that the keyboard supports:
"FN" + "SCRLK" = Backlight On/Off "FN" + "HOME" = "Breathing" On/Off "FN" + "-" = Lower Brightness "FN" + " FN" + "F2" = Lower Volume "FN" + "F3" = Increase Volume "FN" + "F9" = Open Email Application (Thunderbird, in my case)
If your using an Hp Omen if you press Fn and the lighting key you can toggle keyboard backlight on and off using ubuntu studio
For the people with LK Gaming keyboards, use fn + F12 .