Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8259868

Need API to query states of CAPS LOCK and NUM LOCK keys

    XMLWordPrintable

    Details

    • Subcomponent:
    • Compatibility Kind:
      source
    • Compatibility Risk:
      minimal
    • Compatibility Risk Description:
      This is a simple addition of a new static method to a final class.
    • Interface Kind:
      Java API
    • Scope:
      JDK

      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)

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              kcr Kevin Rushforth
              Reporter:
              shadowbug Shadow Bug
              Reviewed By:
              Philip Race
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: