How do I read the status of Num Lock key?

Category: java.awt, viewed: 2K time(s).

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

package org.kodejava.example.awt;

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

public class NumLockState {
    public static void main(String[] args) {
        Toolkit toolkit = Toolkit.getDefaultToolkit();

        //
        // Get the locking state of the Num Lock button. If it is "on"
        // this method return boolean true value.
        //
        boolean isOn = toolkit.getLockingKeyState(KeyEvent.VK_NUM_LOCK);

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

If you run the program you will get an output telling you that the num lock button is active or not active.

Powered by Disqus