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

Option-arrow keys fail to move cursor one word at a time

XMLWordPrintable

    • x86_64
    • os_x

      ADDITIONAL SYSTEM INFORMATION :
      Darwin Gryffindor 21.4.0 Darwin Kernel Version 21.4.0: Fri Mar 18 00:45:05 PDT 2022; root:xnu-8020.101.4~15/RELEASE_X86_64 x86_64

      A DESCRIPTION OF THE PROBLEM :
      Every platform has a modifier that lets users use arrow keys to jump one word at a time instead of one character. For most platforms it's the control key, but on the Mac, it's the option key. This works everywhere on the Mac except for Java applications. It used to work fine in Java up through JDK 1.8, but by JDK 11 it stopped, and hasn't worked since.

      REGRESSION : Last worked in version 8u331

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the executable test case on a Macintosh. It opens two windows, one using Swing and one using the AWT. Each is filled with text. Try navigating the text with arrow keys, and modifiers. In both windows, it works fine using no modifiers and the shift key, which extends the selection. But using the option modifier fails to move the cursor.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The option-left and option-right keystrokes should move the cursor one word at a time, like it does in any other Macintosh application with editable or selectable text.
      ACTUAL -
      The option-left and option-right keystrokes don't move the cursor at all. On Windows, the control modifier works properly. I haven't tried this on Linux.

      ---------- BEGIN SOURCE ----------
      package com.mm.bugs;

      import java.awt.BorderLayout;
      import java.awt.Dimension;
      import java.awt.Frame;
      import java.awt.Panel;
      import java.awt.TextArea;
      import java.awt.event.WindowAdapter;
      import java.awt.event.WindowEvent;
      import javax.swing.JFrame;
      import javax.swing.JLabel;
      import javax.swing.JPanel;
      import javax.swing.JScrollPane;
      import javax.swing.JTextArea;
      import javax.swing.WindowConstants;

      /**
       * Synopsis: option-arrow keys fail to move cursor one word at a time. (Or at all)
       *
       * Description:
       * Every platform has a modifier that lets users use arrow keys to jump one word at a time instead of one character. For most platforms
       * it's the control key, but on the Mac, it's the option key. This works everywhere on the Mac except for Java applications. It used to
       * work fine in Java up through JDK 1.8, but by JDK 11 it stopped, and hasn't worked since.
       *
       * System / OS / Java Runtime Information:
       * Darwin Gryffindor 21.4.0 Darwin Kernel Version 21.4.0: Fri Mar 18 00:45:05 PDT 2022; root:xnu-8020.101.4~15/RELEASE_X86_64 x86_64
       *
       * Steps to Reproduce:
       * Run the executable test case. It opens two windows, one using Swing and one using the AWT. Each is filled with text. Try navigating the
       * text with arrow keys, and modifiers. In both windows, it works fine using no modifiers and the shift key, which extends the selection.
       * But using the option modifier fails to move the cursor.
       *
       * Expected Result:
       * The option-left and option-right keystrokes should move the cursor one word at a time, like it does in any other Macintosh application
       * with editable or selectable text.
       *
       * Actual Result:
       * The option-left and option-right keystrokes don't move the cursor at all. On Windows, the control modifier works properly. I haven't
       * tried this on Linux.
       *
       * @author Miguel Mu\u00f1oz
       */
      @SuppressWarnings({"HardCodedStringLiteral", "MagicNumber", "StringConcatenation", "MagicCharacter", "HardcodedLineSeparator", "TextBlockMigration"})
      public class ArrowKeyBug extends JPanel {
        public static void main(String[] args) {
          JFrame swingFrame = new JFrame("Arrow Key Bug");
          swingFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
          swingFrame.add(BorderLayout.CENTER, new ArrowKeyBug());
          swingFrame.setLocationByPlatform(true);
          swingFrame.pack();
          
          Frame awtFrame = new Frame("AWT Frame");
          Panel awtPanel = new Panel(new BorderLayout());
          TextArea awtTextArea = new TextArea(40, 80);
          awtTextArea.setPreferredSize(new Dimension(400, 300));
          awtTextArea.setText("This is an AWT TextArea, here to determine if this bug exists in the AWT as well. Try hitting the option-left " +
              "and option-right arrow keys, and you'll see that it fails to advance one word at a time, too.");
          awtPanel.add(BorderLayout.CENTER, awtTextArea);
          awtFrame.add(awtPanel);
          
          awtFrame.addWindowListener(new WindowAdapter(){
            @Override
            public void windowClosing(final WindowEvent e) {
              awtFrame.dispose();
            }
          });
          awtFrame.setLocationByPlatform(true);
          awtFrame.pack();
          awtFrame.setVisible(true);
          swingFrame.setVisible(true);
        }
        
        ArrowKeyBug() {
          super(new BorderLayout());
          JTextArea textArea = new JTextArea(40, 80);
          textArea.setLineWrap(true);
          textArea.setWrapStyleWord(true);
          JScrollPane scroller = new JScrollPane(textArea,
              JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
              JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
          add(BorderLayout.CENTER, scroller);
          textArea.setText("Every keyboard-based platform has a keyboard modifier that lets the user use the left and right arrow keys to " +
                           "advance by words instead of characters. Usually, this is the control key, but on the Mac, it's the option key. On " +
                           "each platform, this modifier works everywhere\u2014in basic text editors, word processors, browsers, and so on. " +
                           "Every development environment gives developers text components that support this modifier out-of-the-box. There's " +
                           "no way to turn the feature off. Consequently, support for this modifier is universal. Try it in your browser, your " +
                           "word processor, your webpage design tool. Just click in any editable text box with multiple words, hold down the " +
                           "control key (option key for the Mac), and type the left or right arrow keys. The cursor will advance one word at a " +
                           "time. This works everywhere except here, in a Java application, on the Macintosh. There's a bug in both Swing and " +
                           "awt components, so it doesn't work here on the Mac. It hasn't worked since Java 1.8. \n" +
                           "The shift modifier, to extend the selection, is also universal, and works fine in Java on all platforms. The " +
                           "control modifier, to jump by words, works fine on windows. The only place where this fails is on the Mac. \n" +
                           "For comparison, this code was written to work under Java 11 and 8."
          );
          add(BorderLayout.PAGE_END, new JLabel(" Java Version " + System.getProperty("java.version")));
        }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use Java 1.8

      FREQUENCY : always


            pnarayanaswa Praveen Narayanaswamy
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: