Recently I have purchased a new MacBook Pro 15″ and the first thing I do was install Ubuntu Lucid Lynx on it. After some readying I have all the hardware working properly but the keyboard backlight wasn’t integrated into system and unfortunately after searching on the web for something that could do this, I decide to write a simple script with Bash and use Ubuntu-tweak to bind the keyboard keys to invoke the script. So let’s go…
Preparing our system
Well, before try to install my script on your computer you must have installed the applesmc module on your computer. Don’t worry, if you go to help.ubuntu.com you’ll find specific instructions to install it.
Installing my script at your system
Just right-click on this link, save to your computer and rename it to keyboard-backlight. You can drop it at /usr/bin to execute from wherever you are. Here are the contents of the script If you are curious:
BACKLIGHT=$(cat /sys/class/leds/smc::kbd_backlight/brightness) INCREMENT=15 if [ $UID -ne 0 ]; then echo "Please run this program as superuser" exit 1 fi SET_VALUE=0 case $1 in up) TOTAL=`expr $BACKLIGHT + $INCREMENT` if [ $TOTAL -gt "255" ]; then exit 1 fi SET_VALUE=1 ;; down) TOTAL=`expr $BACKLIGHT - $INCREMENT` if [ $TOTAL -lt "0" ]; then exit 1 fi SET_VALUE=1 ;; total) TEMP_VALUE=$BACKLIGHT while [ $TEMP_VALUE -lt "255" ]; do TEMP_VALUE=`expr $TEMP_VALUE + 1` if [ $TEMP_VALUE -gt "255" ]; then TEMP_VALUE=255; fi echo $TEMP_VALUE > /sys/class/leds/smc::kbd_backlight/brightness done ;; off) TEMP_VALUE=$BACKLIGHT while [ $TEMP_VALUE -gt "0" ]; do TEMP_VALUE=`expr $TEMP_VALUE - 1` if [ $TEMP_VALUE -lt "0" ]; then TEMP_VALUE=0; fi echo $TEMP_VALUE > /sys/class/leds/smc::kbd_backlight/brightness done ;; *) echo "Use: keyboard-light up|down|total|off" ;; esac if [ $SET_VALUE -eq "1" ]; then echo $TOTAL > /sys/class/leds/smc::kbd_backlight/brightness fi
Using keyboard-backlight from command-line
This script could have 4 different uses:
- Increase backlight keyboard: sudo keyboard-backlight up
- Decrease backlight keyboard: sudo keyboard-backlight down
- Increase to total value of keyboard backlight: sudo keyboard-backlight total
- Turn off backlight keyboard: sudo keyboard-backlight off
You can customize the amount of steps by changing the INCREMENT variable inside keyboard-backlight file.
Using it without superuser password
As some advanced user could see, for get this to work we have to run it as superuser (cause we are writing configurations in /sys/).
This impedes us to execute it without being prompted for the superuser password so I have made another workaround. You can allow to admin group’s users to run it via sudo without password prompt. To do this you must add the next lines to the sudoers file:
Cmnd_Alias CMDS = /path/to/your/script/keyboard-backlight %admin ALL = (ALL) NOPASSWD: CMDS
Binding keyboard keys to this script
Now that the script works from command line without password prompting we could bind keyboard keys to this script. I’ll use Ubuntu-Tweak but you can use “compiz-manager” or gconf directly.
In ubuntu-tweak we can tune our keyboard special keys under Personal > Shortcuts Commands, there just set up shortcuts as you can see in the image below.
I hope you enjoy it! And as I say always “Comments, questions, heckles, attacks, praises, and, (most especially) patches and contributions are welcome!”