MacBook Pro keyboard backlight keys on Ubuntu GNU/Linux

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!”

  • Christian

    Hi,
    I just found your script via google and I really like it. I was searching how I could get the special charakters on my MBP’s keyboard like “\” to work in ubuntu. Haven’t found a solution yet, but I have a great idea to improve your script: Add a GUI via zenity and make a scale to adjust the backlight.

  • http://www.mabishu.com Fran Diéguez

    Your wellcome!
    To do exactly what yo want you can use the GUI script available from https://help.ubuntu.com/community/MacBookPro6-2/Lucid#Keyboard backlight without pommed
    Regards

  • http://watchsaw7online.org/ watch saw 7 online

    Thanks for sharing this link, but unfortunately it seems to be offline… Does anybody have a mirror or another source? Please reply to my post if you do!

    I would appreciate if a staff member here at http://www.mabishu.com could post it.

    Thanks,
    John

  • http://watchthesocialnetworkonline.org/ watch the social network online

    Thanks for sharing the link, but unfortunately it seems to be offline… Does anybody have a mirror or another source? Please answer to my post if you do!

    I would appreciate if a staff member here at http://www.mabishu.com could post it.

    Thanks,
    William

  • karim

    the script works on my custom gentoo installation.

  • http://www.comsolvia.se Tomas Carlsson

    Thanks for the script, really nice!
    I’ve added a ‘value’ function, to set the brightness to a specified value. I’m using it in my rc.local to get a fixed brightness when starting the machine (MacBookPro 7.1 with Ubuntu 10.10).

    #!/bin/bash

    # Francisco Diéguez Souto (frandieguez@ubuntu.com)
    # This script is licensed under MIT License.
    # http://www.mabishu.com/blog/2010/06/24/macbook-pro-keyboard-backlight-keys-on-ubuntu-gnulinux/
    #
    # 2010-10-21 tomas@comsolvia.se added ‘value’ function and $USAGE variable
    #
    # 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:
    #
    # Cmnd_Alias CMDS = /usr/local/bin/keyboard-backlight
    # %admin ALL = (ALL) NOPASSWD: CMDS
    #
    # After this you can use this script as follows:
    #
    # Increase backlight keyboard:
    # $ sudo keyboard-backlight up
    # Decrease backlight keyboard:
    # $ sudo keyboard-backlight down
    # Increase to total value backlight keyboard:
    # $ sudo keyboard-backlight total
    # Turn off backlight keyboard:
    # $ sudo keyboard-backlight off
    # Set backlight level to given value (0-255)
    # $ sudo keyboard-backlight value 100
    #
    # 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=15
    USAGE=”Use: keyboard-light up|down|total|off|value x (0-255)”

    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
    ;;
    value)
    if [ ! -n “$2″ ]; then # no value given
    echo $USAGE
    exit 1
    fi
    if [ -z “${2//[0-9]}” ]; then # Only accept numeric values
    echo $2 > /sys/class/leds/smc::kbd_backlight/brightness
    else
    echo $USAGE
    exit 1
    fi
    ;;
    *)
    echo $USAGE
    ;;
    esac

    if [ $SET_VALUE -eq “1” ]; then
    echo $TOTAL > /sys/class/leds/smc::kbd_backlight/brightness
    fi

  • http://www.mabishu.com Fran Diéguez

    Thanks for the improvements! I’ll share this version in the post

  • gramasaurous

    How do I execute the script?

    I put the keyboard-backlight.sh file in /usr/bin

    and then when I try to run a command to modify the backlight state, it says that the command does not exist.

  • http://threestegosaurusmoon.wordpress.com Emmett

    Hey, thanks a lot for the script. I’m using some of the info in it to come up with my own custom version. Very helpful, thanks so much!

  • http://www.mabishu.com Fran Diéguez

    @gramasaurous sorry for the delay, I didn’t get realized that you post a comment here.
    Well, you must give executable rights to the script
    chmod a+x /usr/bin/keyboard-backlight.sh
    should fix it.

    Cheers

  • Pingback: Fixing the screen/LCD brightness keys on a macbook pro, Ubuntu 10.10. | Fejes.ca()

  • khizar

    anyone knows how to do this for windows 7?

    i have broken my brightness key and now i cant see a thing…

    please help me!!!!!!!!!!!!!!!!!!!!!!!!!!!

  • Mark Linux

    intresting .. thanks for the work on the script.. i am trying to figure out how to stop the keyboard backlight from timing out. it can stay on all the time in OS X – but times out after about a minute under Kubuntu Lucid. HHhhmmm…

  • Guest

    i updated this script with a “toggle” function. here it is on pastebin:

    http://pastebin.com/31caG7Wv

  • http://www.facebook.com/rcechinato Rafael Cechinato

    Thanks!! Opensuse 12.2 OK!

  • Nice

    Hello and thank you good sir, working on Fedora 19 Beta, MBA mid 2011 !

  • Harold

    Thanks for the script, it works great. I amended:
    if [ $TOTAL -gt “255” ]; then
    exit 1

    to:
    if [ $TOTAL -gt “255” ]; then
    TOTAL=255

    and:
    if [ $TOTAL -lt “0” ]; then
    exit 1

    to:
    if [ $TOTAL -lt “0” ]; then
    TOTAL=0

    This way you can go down all the way to off and up all the way to max no matter the starting value.

  • A

    Thanks for the sysfs hint! I too made a few modifications[0] to the script and used xbindkeys to bind the backlight keys to the script.

    [0] https://github.com/ckujau/scripts/blob/master/keyboard-backlight.sh
    [1] https://github.com/ckujau/scripts/blob/master/xbindkeysrc

  • http://gravatar.com/lalgjae3252345lajg Jr

    Thanks for your script, it’s working well!:)

  • Rafael Vega

    Works fine in Debian jessie with XFCE on a macbook pro 8,1.