Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Starting out with Java

How do I check the status of the CAPS and NUM lock keys? by Ghodmode
Posted: 14 Jul 06


import java.awt.*;
import java.awt.event.*;

 * This class is an example of one method for checking the
 * state of the CAPS and NUM lock keys before a KeyEvent has
 * been generated.  On some platforms, the
 * Toolkit.getLockingKeyState() method is not supported and
 * throws an UnsupportedOperationException.  The hack
 * creates a new java.awt.Frame, generates some appropriate
 * KeyEvents using java.awt.Robot to be received by an
 * anonymous inner class.  After the hack does it's job, the
 * Frame is disposed to release the memory.
public class capstest {
    public static void main( String [] args ) {
        Frame frame = new Frame( "Checking Locking Key State ..." );

        frame.addKeyListener( new KeyAdapter() {
            public void keyPressed( KeyEvent e ) {
                if ( Character.isLetter(e.getKeyChar()) ) {
                    if ( Character.isUpperCase(e.getKeyChar()) )
                        System.out.println( "CAPS Lock is On" );
                } else {
                    if ( Character.isDigit(e.getKeyChar()) )
                        System.out.println( "NUM Lock is on" );

        // If the frame isn't set visible, the key presses
        // go to the console rather than to the frame
        frame.setVisible( true );

        try {
            Robot robot = new Robot();
            robot.keyPress( KeyEvent.VK_A );
            robot.keyRelease( KeyEvent.VK_A );

            robot.keyPress( KeyEvent.VK_NUMPAD5 );
            robot.keyRelease( KeyEvent.VK_NUMPAD5 );
        } catch ( AWTException e ) {

Back to Java FAQ Index
Back to Java Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close