Chapter 12, Miscellany
#89 Fun with Keyboard Lights
The most obvious enhancement is to use real images for the cursor, as the
many cursor packs available on the Web can attest (type “free cursors” into
Google and see what you get). Animated cursors could also be used to indi-
cate other elements of program state, such as network traffic or the current
tool in use. With a few images and the help of the
setCursor( ) function, you
can do almost anything.
H A C K
Fun with Keyboard Lights Hack #89
Flash the Caps Lock, Num Lock, and Scroll Lock keys for extra user
The AWT and Swing APIs are huge and full of robust components and
frameworks for building big applications. They also have some dark corners
where the lesser-known functions live. While cruising through the JavaDoc
java.awt.Toolkit, I ran across a function I had never noticed before,
despite it being in the API for over four years. This hack explores building a
keyboard busy indicator using the
Toolkit.setLockingKeyState( ) function.
The root class of AWT,
Toolkit, has a very interesting little function:
setLockingKeyState( ). You pass it the KeyEvent for the key you want to lock
down and turn it on or off with the
boolean. For most keyboards, this means
the Caps Lock, Num Lock, and Scroll Lock keys (some keyboards may also
have a Kana lock for Kanji support). Now that you have this nifty little func-
tion, what should you do with it?
My first thought was a busy cursor. If you’ve got three lights in a row, why
not blink them off and on in sequence? The code in Example 12-2 will flip
each light on and off in order, creating a moving bar effect (depending on
the order of your keyboard LEDs).
Figure 12-1. An animated cursor