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

macOS: Option+Up/Down Arrow don't traverse to beginning/end of line in JTextArea

XMLWordPrintable

    • b14
    • x86_64
    • os_x

      ADDITIONAL SYSTEM INFORMATION :
      MacOS Big Sur v11.2.3

      A DESCRIPTION OF THE PROBLEM :
      Text components used to have a platform-specific key combination that moved the cursor one word at a time. This no longer works on the Macintosh. (I am unable to test its operation on other operating systems.) On the Mac it is option-arrow, while on windows and Unix it's control-arrow (left or right). Typing these in a JTextField, JTextArea or similar components no longer move the cursor from word to word. Instead, they do nothing. This make their behavior non-standard. On Windows, Mac, and Unix, in all standard applications, the specified modifier with the left and right arrows move the cursor one word at a time, allowing you to move faster through the text. This broke somewhere between Java 8 and Java 10.

      REGRESSION : Last worked in version 8

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run the test case. Click on the text in either field. Try the arrow keys with no modifiers. Try them with the shift and control (Windows/Unix) or option (Mac) modifiers. Be sure to release all modifier keys between tests. (More on this in the workaround section.) With no modifiers, the cursor will move from character to character, as it should, and with the shift key down, the arrow keys extend the selection, as they should. But typing the arrow keys with the control or option modifier down (Option, on the Mac) does nothing. Note that control-shift or option-shift should extend the selection one word at a time.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Typing ctrl-left or ctrl-right (Windows, Unix) or option-left or option-right (Mac) should move the cursor by a word at a time. The platform modifier with the up and down arrow keys should also move the cursor to the top or bottom of the text field.
      ACTUAL -
      The option-left and option-right keys (Mac) don't move the arrow key at all. Neither do option-up or option-down keys. I don't have a machine to test on Windows or Unix, but it should be tested there, too.

      ---------- BEGIN SOURCE ----------
      import java.awt.GridLayout;
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.JScrollPane;
      import javax.swing.JTextArea;
      import javax.swing.WindowConstants;

      import static javax.swing.JScrollPane.*;

      /**
       * <p>Created by IntelliJ IDEA.
       * <p>Date: 4/28/21
       * <p>Time: 9:10 PM
       *
       * @author Miguel Mu\u00f1oz
       */
      @SuppressWarnings({"HardCodedStringLiteral", "StringConcatenation", "MagicNumber", "TextBlockMigration", "HardcodedLineSeparator"})
      public class AltArrowBug extends JPanel {
      public static void main(String[] args) {
      JFrame frame = new JFrame("Alt-Arrow Bug");
      frame.add(new AltArrowBug());
      frame.pack();
      frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
      frame.setVisible(true);
      }

      private AltArrowBug() {
      super(new GridLayout(0, 1));

      JTextArea textArea = new JTextArea(20, 40);
      textArea.setText("The Alt-Arrow keys (Left and right) should jump from word to word. " +
      "This is what they used to do in Java 8 and before. But starting somewhere from " +
      "Java 9 to Java 10, this stopped working. Now Alt-Arrow doesn't do anything. There " +
      "are two exceptions to this rule. First, if you type shift-Arrow (left or right), " +
      "then add the option key without lifting the shift key, then release the shift key " +
      "without releasing the alt key, then the alt-arrow key will start to work. At this " +
      "point, you can switch back and forth between shift-arrow, shift-alt-arrow, and " +
      "alt-arrow, and the alt-arrow keys will work properly, as long as you keep at least " +
      "one modifier key down. It will last until you release all modifier keys. Of course, " +
      "when you are typing shift-alt-arrow keys, it will both extend the selection and jump " +
      "through the text one word at a time, as it should.\n" +
      "I should point out that the alt-arrow behavior that no longer works is completely " +
      "standard behavior for text editors and text fields in all applications, on Windows, " +
      "Mac, and Linux.\n" +
      "(This text field is edible.)");
      add(wrap(textArea));

      JTextArea textAreaReadOnly = new JTextArea(10, 40);
      textAreaReadOnly.setEditable(false);
      textAreaReadOnly.setText("The behavior is the same for non-editable text fields like this one. " +
      "Try shift-arrow, alt-arrow, and shift-alt-arrow in this field. " +
      "In both fields meta-arrow works fine, moving the text to the beginning or end of the line. " +
      "But the control-arrow keys don't do anything, which is not standard. Elsewhere, control-arrow " +
      "does the same thing as meta-arrow. I don't know if this ever worked.\n");
      add(wrap(textAreaReadOnly));
      }

      private JScrollPane wrap(JTextArea textArea) {
      textArea.setWrapStyleWord(true);
      textArea.setLineWrap(true);
      return new JScrollPane(textArea, VERTICAL_SCROLLBAR_ALWAYS, HORIZONTAL_SCROLLBAR_NEVER);
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      There are keystroke combinations that will temporarily activate the proper option-arrow behavior that are described in the text displayed by the test case. The simplest workaround is this: When holding down the option key, briefly press and release the shift key. Then, without releasing the option key, start typing the arrow keys. They will move the cursor one word at a time. This will last until you release all the modifier keys.
      If you want to use shift-arrow to extend the selection by one word at a time, it will work if you press the option key first, then add the shift key. But it won't work in the reverse order. (Most users probably won't stumble across this workaround.)
      The workaround doesn't work in Java 12 but works in Java 15. I don't know about 13 and 14.

      FREQUENCY : always


            psadhukhan Prasanta Sadhukhan
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: