How do I read the status of Caps Lock key?

This example show you how to detect if the Caps Lock key is in active mode.

package org.kodejava.example.awt;

import java.awt.Toolkit;
import java.awt.event.KeyEvent;

public class CapsLockState {
    public static void main(String[] args) {
        //
        // Get the locking state of the Caps Lock button. If it is "on" this method
        // return boolean true value.
        //
        boolean isOn = Toolkit.getDefaultToolkit().getLockingKeyState(KeyEvent.VK_CAPS_LOCK);

        System.out.println("CapsLock button is " + (isOn ? "on" : "off"));
    }
}

Wayan

Programmer, runner, recreational diver, live in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, Hibernate / JPA.

Leave a Reply