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

[SwingNode] getKeyLocation not correctly implemented with JavaFX

    XMLWordPrintable

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P4
    • Resolution: Approved
    • Fix Version/s: openjfx11
    • Component/s: javafx
    • Labels:
      None
    • Subcomponent:
    • Compatibility Risk:
      minimal
    • Interface Kind:
      Java API
    • Scope:
      JDK

      Description

      Summary

      New API will be introduced to make sure right and left version of CTRL, SHIFT, ALT keys can be distinguished.

      Problem

      Some keys like CTRL, SHIFT, ALT etc can have left or right version. But, FX does not support distinguishing between left and right key version.

      Solution

      Have new KeyEvent API with keyLocation parameter and getKeyLocation() API so that it can be used to distinguish between left and right version of the keys. Also, the numpad keys can be distinguished.

      Specification

      modules/javafx.graphics/src/main/java/javafx/scene/input/KeyEvent.java

      /**
       * Enum to distinguish between left, right, standard version or
       * numeric keypad of key.
       *
       * @since 10
       */
      public enum KeyLocation {
          /**
           * Indeterminate or not relevant keyLocation
           * {@code KEY_TYPED} events do not have a keyLocation; this value
           * is used instead.
           */
          KEY_LOCATION_UNKNOWN(0),
      
          /**
           * Key location indicating that the key pressed or released
           * is not distinguished as the left or right version of a key,
           * and did not originate on the numeric keypad (or did not
           * originate with a virtual key corresponding to the numeric
           * keypad)
           */
          KEY_LOCATION_STANDARD(1),
      
          /**
           * Key location indicating that the key pressed or released is in
           * the left key location (there is more than one possible location
           * for this key).  Example: the left shift key.
           */
          KEY_LOCATION_LEFT(2),
      
          /**
           * Key location indicating that the key pressed or released is in
           * the right key location (there is more than one possible location
           * for this key).  Example: the right shift key.
           */
          KEY_LOCATION_RIGHT(3),
      
          /**
           * Key location indicating that the key event originated on the
           * numeric keypad or with a virtual key corresponding to the
           * numeric keypad.
           */
          KEY_LOCATION_NUMPAD(4);
      
          int location;
          private KeyLocation(int location) {
              this.location = location;
          }
      
          /**
           * Returns integer value of corresponding keyLocation enum constant.
           * This returns the same value as corresponding AWT keyLocation value.
           *
           * @return integer value of corresponding enum constant.
           */
          public int toInt() {
              return this.location;
          }
      }
      
      
      
      /**
       * Constructs a new {@code KeyEvent} event from the specified parameters.
       * @param source the source of the event. Can be null.
       * @param target the target of the event. Can be null.
       * @param eventType The type of the event.
       * @param character The character or sequence of characters associated with the event
       * @param text A String describing the key code
       * @param code The integer key code
       * @param shiftDown true if shift modifier was pressed.
       * @param controlDown true if control modifier was pressed.
       * @param altDown true if alt modifier was pressed.
       * @param metaDown true if meta modifier was pressed.
       * @param keyLocation Identifies the key location.  The only legal
       *        values are {@code KEY_LOCATION_UNKNOWN},
       *        {@code KEY_LOCATION_STANDARD}, {@code KEY_LOCATION_LEFT},
       *        {@code KEY_LOCATION_RIGHT}, and {@code KEY_LOCATION_NUMPAD}.
       *
       * @since 10
       */
      public KeyEvent(@NamedArg("source") Object source,
                      @NamedArg("target") EventTarget target,
                      @NamedArg("eventType") EventType<KeyEvent> eventType,
                      @NamedArg("character") String character,
                      @NamedArg("text") String text,
                      @NamedArg("code") KeyCode code,
                      @NamedArg("shiftDown") boolean shiftDown,
                      @NamedArg("controlDown") boolean controlDown,
                      @NamedArg("altDown") boolean altDown,
                      @NamedArg("metaDown") boolean metaDown,
                      @NamedArg("keyLocation") KeyLocation keyLocation) {
      
      
      /**
       * Constructs a new {@code KeyEvent} event from the specified parameters,
       * with a {@code null} source and target.
       * @param eventType The type of the event.
       * @param character The character or sequence of characters associated with the event
       * @param text A String describing the key code
       * @param code The integer key code
       * @param shiftDown true if shift modifier was pressed.
       * @param controlDown true if control modifier was pressed.
       * @param altDown true if alt modifier was pressed.
       * @param metaDown true if meta modifier was pressed.
       * @param keyLocation Identifies the key location.  The only legal
       *        values are {@code KEY_LOCATION_UNKNOWN},
       *        {@code KEY_LOCATION_STANDARD}, {@code KEY_LOCATION_LEFT},
       *        {@code KEY_LOCATION_RIGHT}, and {@code KEY_LOCATION_NUMPAD}.
       * @since 10
       */
      public KeyEvent(@NamedArg("eventType") EventType<KeyEvent> eventType,
                      @NamedArg("character") String character,
                      @NamedArg("text") String text,
                      @NamedArg("code") KeyCode code,
                      @NamedArg("shiftDown") boolean shiftDown,
                      @NamedArg("controlDown") boolean controlDown,
                      @NamedArg("altDown") boolean altDown,
                      @NamedArg("metaDown") boolean metaDown,
                      @NamedArg("keyLocation") KeyLocation keyLocation) {
      
        private final  KeyLocation keyLocation;
      
      /**
       * Returns the Keylocation enum constant of the key that originated this key event.
       *
       * Some keys occur more than once on a keyboard, e.g. the left and
       * right shift keys.  Additionally, some keys occur on the numeric
       * keypad.  This provides a way of distinguishing such keys.
       *
       * @return the location of the key that was pressed or released.
       *         Always returns {@code KEY_LOCATION_UNKNOWN} for
       *         {@code KEY_TYPED} events.
       * @since 10
       */
      public KeyLocation getKeyLocation() {
          return keyLocation;
      }

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                psadhukhan Prasanta Sadhukhan
                Reporter:
                duke J. Duke (Inactive)
                Reviewed By:
                Kevin Rushforth
              • Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: