I was browsing around a little while ago and it occurred to me that the keyboard backlight on my MacBook Pro 5,4 wasn’t working under Ubuntu 10.10 Maverick Meerkat. I found a few resources online to help with the problem, and it ended up teaching me a lot. I wrote a script that changes the numeric string stored in /sys/class/leds/smc::kbd_backlight/brightness, then set the script to be run every time the keylight increment/decrement buttons on the keyboard are pressed. Here are the steps I took, in case you want to try this for yourself.
I worked up a shell script that, depending on the string passed from the command line, either increments, decrements, sets to zero, or sets to 100% the value of the backlight brightness.
Go ahead and use the code, or write your own, I don’t care. You can either copy and paste from here into a file called
keylight in /usr/bin, or download the file here.
Once you have /usr/bin/keylight on your system, run
sudo chmod +x /usr/bin/keylight to make the script executable. You’ll know that you forgot this step if you get a “command not found” error when you try to run it.
To test the script, run
sudo keylight full. The keyboard backlight should come on. To turn it off, run
sudo keylight off. I use an alias to avoid the necessity of sudoing every time – that is, I added the line
alias keylight='sudo keylight' to the /home/emmett/.bashrc file. Still, a password is required when running the script. Since we want this to be controlled with the keyboard, we have to override that somehow. This can be accomplished by adding the following lines to /etc/sudoers (run the command
sudo visudo to edit this file).
Cmnd_Alias KEY = /usr/bin/keylight
%admin ALL = (ALL) NOPASSWD: KEY
(it’s hard to see, but there is an underscore between Cmnd and Alias – that is, Cmnd_Alias)
These lines tell the computer that, when running the keylight command, members of the admin group do not need to enter a password. With that accomplished, all that’s left is to create new keyboard shortcuts for the script. In System->Preferences->Keyboard Shortcuts, click “Add”. Fill the “Name” field with something like “Keylight up”, and enter
sudo keylight up into the “Command” field. (Don’t forget the sudo!) Click OK, and change the hotkey for the command to the F6 key (XF86KbdBrightnessUp). Repeat the process for keylight down, keylight full, and keylight off. I use the F5 key for down, and add Ctrl for full and off.
Follow this process, and the backlight on your keyboard should work like it does under Mac OS X. At least, it does for me. Feel free to comment with feedback, comments, or additions you make to my code. Thanks for reading!