SwingHack: keyboard spinner

While crusing through the AWT/Swing documentation for another project I ran across a method I never knew existed: Toolkit.setLockingKeyState(int keyCode, boolean on). It's been there since 1.3 (which is what, 3 years old now) but I never noticed it before. Hmm, I thought. What could I use that for?

Well, lately I've been doing database apps that sometimes have long access times, so why not create an analog version of the busy cursor. Behold!! The useless but amusing keyboard spinner. This app will open a window with a button and rotate the keyboard lights in order (if your lights happen to be in the order of numlock, caps, scroll as on my Compaq keyboard).

Anyway, thought this would be fun before we go into our three day vacations (or at least those of you in the States. The rest of you will always be one day ahead of us. :)

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.Thread;

public class KeyLightTest {
    public static void main(String[] args) {
        // create the spinner
        final SpinnerThread spinner = new SpinnerThread();

        //create a frame and button
        JFrame frame = new JFrame();
        JButton button = new JButton("Quit");
        frame.getContentPane().add(button);
        frame.pack();
        // hook up an action to stop the spinner and quit
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                spinner.quit();
                System.exit(0);
            }
        });

        // start'er up!
        spinner.start();
        frame.show();
    }
}

class SpinnerThread extends Thread {
    private boolean go;
    public void quit() {
        go = false;
    }
    public void run() {
        go = true;
        // get a toolkit
        Toolkit tk = Toolkit.getDefaultToolkit();

        // 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);

        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++;
        }

        // 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);
    }
}
Talk to me about it on Twitter

Posted August 28th, 2003

Tagged: java swing-hacks java.net