Thursday, April 17, 2008 at 11:50 PM
Over the last few years, on my websites and in my book, we have discussed how to access or manipulate several interesting Mac components: the sudden motion sensor, trusted platform module, infrared remote control, system management controller, traditional input devices, the backlit keyboard lights, the ambient light sensor, and so on. (Not every Mac has all of these components.) The most popular of these by far turned out to be the sudden motion sensor, which went on to be a building block for many, many cool programs.
Speaking of lights, there are some lights on your computer that we haven't talked about yet. The caps lock and num lock LEDs on your keyboard (not all keyboards have these) are examples. These LEDs can be manipulated through software: you can both query and alter their state from your own programs.
If you have an irrepressible urge to turn these LEDs on or off through software, here is a program that shows you how. (Note that the program only manipulates the LEDs — it will not actually cause caps lock or num lock to be engaged.) The program also serves as an example of how to do user-space Human Interface Device (HID) programming through the I/O Kit.
The program should work whether you have a wired USB keyboard or a Bluetooth wireless keyboard. Compile and run the program as follows.
$ gcc -Wall -o keyboard_leds keyboard_leds.c \
-framework IOKit -framework CoreFoundation
$ ./keyboard_leds -h
Usage: keyboard_leds [OPTIONS...], where OPTIONS is one of the following
-c[1|0], --capslock[=1|=0] get or set (on=1, off=0) caps lock LED
-h, --help print this help message and exit
-n[1|0], --numlock[=1|=0] get or set (on=1, off=0) num lock LED
$ ./keyboard_leds -c # query caps lock LED state
$ ./keyboard_leds -c1 # turn caps lock LED on
$ ./keyboard_leds -c0 # turn caps lock LED off