RFR: 8259680: Need API to query states of CAPS LOCK and NUM LOCK keys

Frederic Thevenet fthevenet at openjdk.java.net
Sun Jan 24 14:52:42 UTC 2021

On Sat, 23 Jan 2021 23:11:10 GMT, Nir Lisker <nlisker at openjdk.org> wrote:

>> 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.
>> 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.
>> I have provided both an automated Robot test and a manual test. The latter is needed primarily because we can't set the CAPS lock on Mac using Robot, but also because we want  way to test the case where the user has enabled CAPS lock before the program starts.
> modules/javafx.graphics/src/main/java/com/sun/glass/ui/Application.java line 764:
>> 762:             default:
>> 763:                 return Optional.empty();
>> 764:         }
> This could be a switch expression:
> return switch (lockState) {
>     case KeyEvent.KEY_LOCK_OFF -> Optional.of(false);
>     case KeyEvent.KEY_LOCK_ON -> Optional.of(true);
>     default -> Optional.empty();
> }
> Also, do we use a space after `switch`? I see many instances both with and without in the current source.

Do we plan to drop support for JDK11 for OpenJFX 17? If not, we'll need to keep to the classic Switch statement, as Switch expressions were only introduced in JDK12 or 13 as a preview.
That said, since 17 is going to be the next LTS release, maybe the plan is to bump the dependency for OpenJFX then?


PR: https://git.openjdk.java.net/jfx/pull/385

More information about the openjfx-dev mailing list