How to check the status of a caps lock key..?  

You can do it without using key event, by creating an artificial key event using java.awt.Robot class to generate a "simple caracter" key press (for instance KeyEvent.VK_A).
Sample Source Code -- May not be accurate but the concept is as follows.

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

public class Test {

      public static void main(String[] args) throws AWTException {
     
            // create AWT component
            Frame f = new Frame();
            // handle component's keyPressed event
            f.addKeyListener(new KeyAdapter() {      public void
keyPressed(KeyEvent ev) {System.out.println
                                               
      (Character.isUpperCase( ev.getKeyChar()) ?       "Caps Lock
ON" :"Caps Lock OFF");}           
            });
            // make component visible (otherwise the Robot won't
work)
            f.show();
            // create Robot
            Robot robot = new Robot();
            // generate simple caracter key press
            robot.keyPress(KeyEvent.VK_A);     
     
      }

}

Showing Answers 1 - 9 of 9 Answers

hobocat

  • Dec 20th, 2017
 

There is same bug: when I changed the capslock's status,the check result change nothing.

Code
  1. public static void main(String[] args) throws InterruptedException {  

  2.         while(true) {

  3.                 Thread.sleep(1000);

  4.                 System.out.println(Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK));

  5.         }

  6.     }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions