446
|
Chapter 12, Miscellany
#89 Fun with Keyboard Lights
HACK
And More...
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
#89
Fun with Keyboard Lights Hack #89
Flash the Caps Lock, Num Lock, and Scroll Lock keys for extra user
feedback.
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
for
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
Fun with Keyboard Lights #89
Chapter 12, Miscellany
|
447
HACK
SpinnerThread is a Runnable implementation, meaning you can launch it with
new Thread(new SpinnerThread()).start( ). The run( ) method starts an infi-
nite loop, ending only when the
quit( ) method is called. The first thing to
notice is that the code saves the existing state of the buttons so that it can
restore them later. If you had Caps Lock on you’d probably still want it on
once the busy cursor leaves. Next, it sets all of the key states to false. This
puts them into a known position so that the animation looks right:
int key = -1;
boolean state = false;
// loop through 100 times
int counter = 0;
while(go) {
// select each key every 3rd time
if(counter%3 == 0) { key = KeyEvent.VK_NUM_LOCK; }
if(counter%3 == 1) { key = KeyEvent.VK_CAPS_LOCK; }
if(counter%3 == 2) { key = KeyEvent.VK_SCROLL_LOCK; }
// flip the state
state = tk.getLockingKeyState(key);
tk.setLockingKeyState(key,!state);
// sleep for 500 msec
try { Thread.currentThread( ).sleep(500);
} catch (InterruptedException ex) {}
// increment counter
counter++;
}
Example 12-2. Lights, camera, action
class SpinnerThread extends Thread {
private boolean go;
public void quit( ) {
go = false;
}
public void run( ) {
go = true;
// get a toolkit
Toolkit tk = Toolkit.getDefaultToolkit( );
// save the old key states
boolean old_num, old_caps, old_scroll;
old_num = tk.getLockingKeyState(KeyEvent.VK_NUM_LOCK);
old_caps = tk.getLockingKeyState(KeyEvent.VK_CAPS_LOCK);
old_scroll = tk.getLockingKeyState(KeyEvent.VK_SCROLL_LOCK);
// set all keys to off
tk.setLockingKeyState(KeyEvent.VK_NUM_LOCK,false);
tk.setLockingKeyState(KeyEvent.VK_CAPS_LOCK,false);
tk.setLockingKeyState(KeyEvent.VK_SCROLL_LOCK,false);

Get Swing Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.