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

Ambarish Rapte arapte at openjdk.java.net
Fri Jan 29 12:46:46 UTC 2021

On Mon, 25 Jan 2021 23:08:58 GMT, Kevin Rushforth <kcr 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.
> Kevin Rushforth has updated the pull request incrementally with one additional commit since the last revision:
>   Use XkbGetNamedIndicator on Linux instead of relying on the (unreliable) GDK method.

Looks good to me, verified tests on all three platforms.
Left minor comments.

modules/javafx.graphics/src/main/java/com/sun/javafx/tk/Toolkit.java line 878:

> 876: 
> 877:     /**
> 878:      * Return the lock state for the given keyCode.

Minor typo: Return -> Returns

modules/javafx.graphics/src/main/native-glass/mac/GlassKey.m line 407:

> 405:   (JNIEnv * env, jobject obj, jint keyCode)
> 406: {
> 407:     NSUInteger modifierFlags = [NSEvent modifierFlags];

If this line is moved just before return statement(Line#418),  then we can avoid executing this call in case of invalid keyCode.

modules/javafx.graphics/src/main/native-glass/gtk/glass_key.cpp line 381:

> 379:     }
> 380: 
> 381:     Atom atom = None;

The documentation of [XInternAtom()](https://www.x.org/releases/X11R7.5/doc/man/man3/XInternAtom.3.html) does mention that this function returns an atom identifier associated with the provided atom name. So the variable name 'atom' does sound good enough. But would it be good to rename this variable to something like 'keyCodeAtom'. I am Ok either way.


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

More information about the openjfx-dev mailing list