The Source for Java Technology Collaboration
User: Password:



Joshua Marinacci

Joshua Marinacci's Blog

SwingHack: keyboard spinner

Posted by joshy on August 28, 2003 at 12:56 PM | Comments (7)

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

Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Does not work on Linux either...
    Hi!

    I just compiled your keyboard spinner but on Linux I get the following:

    java.lang.UnsupportedOperationException: Toolkit.setLockingKeyState
    at java.awt.Toolkit.setLockingKeyState(Toolkit.java:1226)
    at SpinnerThread.run(KeyLightTest.java:41)

    My Java version is:

    java version "1.4.0"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
    Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

    Posted by: borisklug on September 01, 2003 at 12:08 AM

  • Does not work on Linux either...
    You tried to run the program as root? Under any unix, a regular user probably don't have permissions to directly access the hardware. That or Sun didn't implement this in the Linux JDK (bad, bad sun).

    Posted by: mbrito on September 01, 2003 at 12:45 PM

  • Does not work on Linux either...
    I'd assume the latter (that Sun did not implement it for Linux). If it were a root issue, you should probably get a SecurityException rather than an UnsupportedOperationException.

    I'll have to try it on Mac OS X when I get home.

    Posted by: jimothy on September 02, 2003 at 05:54 AM

  • Does not work on Linux either...
    I doesn't work under Sun's Linux SDK 1.4.2, root or non-root.
    Worked fine under Windows XP.

    Posted by: ktorn on September 02, 2003 at 08:11 AM

  • Applet version
    Here is the applet version of the above code.

    http://webdev.apl.jhu.edu/~kalman/Cute.html

    The actual code is in

    http://webdev.apl.jhu.edu/~kalman/cute/

    Posted by: kalman on September 02, 2003 at 08:53 AM

  • brightened my day ;)
    hah cute

    Posted by: noelisevil on September 24, 2003 at 01:22 AM

  • wow power leveling
    wow powerleveling
    wow power leveling
    wow gold
    wow items
    feelingame.com
    wow tips
    Most Valuable WOW Power Leveling Service
    wow power leveling faq
    cheap wow power leveling
    wow power leveling
    wow powerleveling
    wow power lvl

    Posted by: wowleveling on December 13, 2007 at 12:30 AM





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds