Keyboard backlight command by percentage

Tags:

Answer: 1

33 hours ago

I want to make a keyboard shortcut to increase and the decrease the keyboard backlight brightness.

The command I have for full brightness is:

echo -n 100 > /sys/class/leds/chromeos\:\:kbd_backlight/brightness

No keyboard led brightness (off):

echo -n 0 > /sys/class/leds/chromeos\:\:kbd_backlight/brightness

If I wanted to get the current brighness I would:

cat /sys/class/leds/chromeos\:\:kbd_backlight/brightness

Whats the simplest way to increase and decrease the values by a percantage, like 12%. This seems like a fairly common task.

Answer: 2

2 hours ago

Script to increase and decrease brightness:

#!/bin/bash

step=12
file=/sys/class/leds/chromeos\:\:kbd_backlight/brightness

case "$1" in
    -i|--increase) ((val = +step));;
    -d|--decrease) ((val = -step));;
esac

if !((val)); then
    echo "Increase or decrease screen brighness"
    echo "Usage: ${0##*/} --increase | --decrease"
    exit
fi

read -r cur < "$file"    
((val = cur + val))

if ((val <   0)); then ((val =   0)); fi
if ((val > 100)); then ((val = 100)); fi

printf '%d' "$val" > "$file"

printf 'Before: %3d\n' "$cur"
printf 'After : %3d\n' "$val"

Could be a lot simpler for a keyboard shortcut, but I decided to make it a stand-alone script with help and args, just for fun :)

To run:

  • save it somewhere, for example ~/keyboard-brightness
  • set execute permission: chmod +x ~/keyboard-brightness
  • ~/keyboard-brightness --increase
  • ~/keyboard-brightness --decrease

Now just add them as keyboard shortcuts

Added by: Darryl Wolf IV

Popular Search

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 1 2 3 4 5 6 7 8 9