Detecting CAPS LOCK in Java

From JDK1.3, you should normally be able to get the state using the Toolkit class as shown in following example.

Main.java

import java.awt.*;
 
public class Main
{
   public static void main(String []args) {
      Toolkit tk = Toolkit.getDefaultToolkit();
      System.out.println(tk.getLockingKeyState(java.awt.event.KeyEvent.VK_CAPS_LOCK));
 
      System.exit(1);
   }
}

However, check out this bug.