How do I turn on the Num Lock button?

The example below show you how to activate the Num Lock button programmatically. Setting the locking state to Boolean.TRUE will turn the Num Lock on.

package org.kodejava.awt;

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

public class TurnNumLockOn {
    public static void main(String[] args) {
        // Gets the default toolkit.
        Toolkit toolkit = Toolkit.getDefaultToolkit();

        // Update the locking state for num lock button to true
        // will turn the num lock on.
        toolkit.setLockingKeyState(KeyEvent.VK_NUM_LOCK, Boolean.TRUE);
    }
}

How do I read the status of Num Lock key?

This example show you how to detect if the Num Lock key is in active mode. If you run the program you will get an output telling you that the Num Lock button is active or not active.

package org.kodejava.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. This method
        // return boolean true value if it is "on".
        boolean isOn = toolkit.getLockingKeyState(KeyEvent.VK_NUM_LOCK);

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