JDK-8259868 : Need API to query states of CAPS LOCK and NUM LOCK keys
  • Type: CSR
  • Component: javafx
  • Sub-Component: scenegraph
  • Priority: P3
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 8-pool,openjfx11,openjfx17
  • Submitted: 2021-01-16
  • Updated: 2021-01-29
  • Resolved: 2021-01-29
Related Reports
CSR :  
Description
Summary
-------

Add a new method, `Platform::isKeyLocked`, to query the state of the CAPS LOCK and NUM LOCK keys.

Problem
-------

The JavaFX API does not provide a way to get the state of CAPS LOCK or NUM LOCK on the keyboard. Being able to read the lock state would allow an application to inform the user that caps lock was enabled for passwords or other usages where the keyboard input might not be echoed. It would also allow an application to do spell checking / auto-correction that might ordinarily be skipped when typing all upper-case letters.

Solution
--------

We need an equivalent JavaFX API to the existing AWT `java.awt.ToolKit::getLockingKeyState` method. A natural place to put this in JavaFX is in the `javafx.application.Platform` class, so we propose to create a new `Platform::isKeyLocked` method, which will take a `KeyCode` -- either `CAPS` or `NUM_LOCK` -- and return an `Optional<Boolean>` indicating whether or not that key is in the locked or "on" state. If we can't read the key state on a particular platform, we will return `Optional.empty()`, rather than throwing a runtime exception as AWT does.

Specification
-------------

Add the following method to `javafx.application.Platform`:

```
    /**
     * Returns a flag indicating whether the key corresponding to {@code keyCode}
     * is in the locked (or "on") state.
     * {@code keyCode} must be one of: {@link KeyCode#CAPS} or
     * {@link KeyCode#NUM_LOCK}.
     * If the underlying system is not able to determine the state of the
     * specified {@code keyCode}, an empty {@code Optional} is returned.
     * If the keyboard attached to the system doesn't have the specified key,
     * an {@code Optional} containing {@code false} is returned.
     * This method must be called on the JavaFX Application thread.
     *
     * @param keyCode the {@code KeyCode} of the lock state to query
     *
     * @return the lock state of the key corresponding to {@code keyCode},
     * or an empty {@code Optional} if the system cannot determine its state
     *
     * @throws IllegalArgumentException if {@code keyCode} is not one of the
     * valid {@code KeyCode} values
     *
     * @throws IllegalStateException if this method is called on a thread
     * other than the JavaFX Application Thread
     *
     * @since 17
     */
    public static Optional<Boolean> isKeyLocked(KeyCode keyCode)
```

Comments
Moving to Approved, including for update releases with appropriate @since tags.
29-01-2021

Moving to Provisional. ("Scroll lock 4ever!")
26-01-2021