Fixing the screen/LCD brightness keys on a macbook pro, Ubuntu 10.10.

If you’ve been following my posts, you’ll know I have a macbook pro running Ubuntu Linux, and you’ll also know I love tweaking things. I’m not obsessive about tweaking, but if something could work better, I’d like it to work better. So after a month of having 4 dead keys on my laptop, I figured I had to do something about it.

They keys are:

  • keyboard back light brightness up
  • keyboard back light brightness down
  • monitor brightness up
  • monitor brightness down

They’re hardly the most important buttons on a keyboard, but I figured I’d let them sit idle long enough.

Getting them to work turns out to be a relatively simple.  Initially, I’d just followed the instructions here to get the keyboard LED brightness working, and that did a decent job…. but not perfect.  It didn’t actually let you get the brightness all the way to zero or to 255, the max brightness.   Thus, I modified the script:

(You can download it here)

#!/bin/bash
# Francisco Diéguez Souto (frandieguez@ubuntu.com)
# This script is licensed under MIT License.
# Modified by Anthony Fejes (apfejes@gmail.com)
#
# This program just modifies the value of backlight keyboard for Apple Laptops
# You must run it as root user or via sudo.
# As a shortcut you could allow to admin users to run via sudo without password
# prompt. To do this you must add sudoers file the next contents:
#
#   ALL = (ALL) NOPASSWD: /usr/sbin/keyboard-backlight
#
# You must then install the script in the path given above, eg, /usr/sbin/
# If you chose another path, then the location in the sudoers file must reflect 
# that path.
#
# After this you can use this script as follows:
#
#     Increase backlight keyboard:
#           $ sudo keyboard-backlight up
#     Decrease backlight keyboard:
#           $ sudo keyboard-backlight down
#
# You can customize the amount of backlight by step by changing the INCREMENT
# variable as you want it.

BACKLIGHT=$(cat /sys/class/leds/smc::kbd_backlight/brightness)
INCREMENT=10

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 [ $BACKLIGHT -eq "255" ]; then
                exit 1
        fi
        if [ $TOTAL -gt "255" ]; then
            TOTAL="255"
        fi
        echo $TOTAL > /sys/class/leds/smc::kbd_backlight/brightness
        ;;
    down)
        TOTAL=`expr $BACKLIGHT - $INCREMENT`
        if [ $BACKLIGHT -eq "0" ]; then
                exit 1
        fi
        if [ $TOTAL -lt "0" ]; then
            TOTAL="0"
        fi
        echo $TOTAL > /sys/class/leds/smc::kbd_backlight/brightness
        ;;
    *)
        echo "Use: keyboard-light up|down"
        ;;
esac

Following the instructions in the header to add this into the sudoers file, you can then go to your windowing environment and associate the keys with the command. In my case, I went to the Settings menu in the KDE launcher, clicked on the System Settings toolbox and went into the Gestures and Shortcuts menu. I created a new group brightness controls in the custom input action settings, then used the menu to create new global shortcuts and picked the “command/URL” type. At this point, all you need to do is move to the “Trigger” tab, click on the key you want to associate with each command, then enter the commands into the action tab. (The commands are:

sudo /usr/sbin/keyboard-backlight up

and

sudo /usr/sbin/keyboard-backlight down

Getting the screen brightness to work wasn’t that much harder. The script looks like:

(You can download it here)

#!/bin/bash                                                                                            
                                                                                                       
# Anthony Fejes (apfejes@gmail.com)                                                                    
# Template taken from post by Fran Diéguez at                                                          
# http://www.mabishu.com/blog/2010/06/24/macbook-pro-keyboard-backlight-keys-on-ubuntu-gnulinux/       
#                                                                                                      
# This program just modifies the value of video brightness for Apple Laptops                           
# You must run it as root user or via sudo.
# As a shortcut you could allow to admin users to run via sudo without password
# prompt. To do this you must add sudoers file the next contents:
#
#   ALL = NOPASSWD: /usr/sbin/mbp_backlight

# After this you can use this script as follows:
#
#     Increase backlight keyboard:
#           $ sudo mbp_backlight up
#     Decrease backlight keyboard:
#           $ sudo mbp_backlight down
#

BACKLIGHT=$(cat /sys/devices/virtual/backlight/mbp_backlight/brightness)
MAX=$(cat /sys/devices/virtual/backlight/mbp_backlight/max_brightness)
MIN=4
INCREMENT=1

if [ $UID -ne 0 ]; then
    echo "Please run this program as superuser"
    exit 1
fi

case $1 in

    up)
        TOTAL=`expr $BACKLIGHT + $INCREMENT`
        if [ $BACKLIGHT -eq $MAX ]; then
                exit 1
        fi
        if [ $TOTAL -gt $MAX ]; then
            let TOTAL=MAX
        fi
        echo $TOTAL > /sys/devices/virtual/backlight/mbp_backlight/brightness
        ;;
    down)
        TOTAL=`expr $BACKLIGHT - $INCREMENT`
        if [ $BACKLIGHT -eq $MIN ]; then
                exit 1
        fi
        if [ $TOTAL -lt $MIN ]; then
            let TOTAL=MIN
        fi
        echo $TOTAL > /sys/devices/virtual/backlight/mbp_backlight/brightness
        ;;
    *)
        echo "Use: mbp_backlight up|down"
        ;;
esac

And method is identical to that above, associating the appropriate keys to the command:

sudo /usr/sbin/mbp_brightness up

and

sudo /usr/sbin/mbp_brightness down

And I’m now happily able to use all of the keys on my keyboard!