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"));
}
}
Latest posts by Wayan (see all)
- How do I use Proxy class to configure HTTP and SOCKS proxies in Java? - March 27, 2025
- How do I retrieve network interface information using NetworkInterface in Java? - March 26, 2025
- How do I work with InetAddress to resolve IP addresses in Java? - March 25, 2025
Wayan, thanks It helped me 🙂
Just thought I’d mention that this was not helpful to me. The Java implementation of getLockingKeyState() is bugged (see the bug parade). It only works if the java app has the focus always.
I’d hoped for help with this. On to the next site.