How do I read the status of Scroll Lock key?

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

package org.kodejava.example.awt;

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

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

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

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

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

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