Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 

Keyboard Light Test Java code example - Click here to copy ->>>

   Can't find what you're looking for? Try our search:

Really working examples categorized by API, package, class. You can compile and run our examples right away! Not from source code for Java projects - only working examples! Copy, compile and run!
------------------

Code:

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

public class KeyboardLightTest {
    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("Stop");
        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();
            }
        });

        // 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();
        boolean old_num, old_caps, old_scroll;

        // save the old key states
        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);

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

        // restore the key settings
        tk.setLockingKeyState(KeyEvent.VK_NUM_LOCK,old_num);
        tk.setLockingKeyState(KeyEvent.VK_CAPS_LOCK,old_caps);
        tk.setLockingKeyState(KeyEvent.VK_SCROLL_LOCK,old_scroll);
    }
}


/*   Swing Hacks
 *   Tips and Tools for Killer GUIs
 * By Joshua Marinacci, Chris Adamson
 *   First Edition June 2005
 *   Series: Hacks
 *   ISBN: 0-596-00907-0
 *   http://www.oreilly.com/catalog/swinghks/
 */

 
 



References.

The list of classes which were used on this page you can find below. The links to Java API contain official SUN documentation about all used classes.

  java.lang.Thread

  javax.swing.JFrame

  javax.swing.JButton

  java.awt.event.ActionListener

  java.awt.event.ActionEvent

  java.awt.Toolkit

  java.lang.InterruptedException




[ Go Back ]



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy