JDK-4501485 : Alt-arrow keys enters strange characters in TextComponents
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.4.0,1.4.1
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt,windows_2000
  • CPU: x86
  • Submitted: 2001-09-10
  • Updated: 2002-10-09
  • Resolved: 2002-10-09
Related Reports
Duplicate :  
Relates :  
Relates :  
Description

Name: ddT132432			Date: 09/10/2001


java version "1.4.0-beta2"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta2-b77)
Java HotSpot(TM) Client VM (build 1.4.0-beta2-b77, mixed mode)

/*
  Program to illustrate the Alt-Arrow bug.

Under JDK 1.3 and earlier, if I hold down the alt key and type an arrow,
nothing happens. Nothing should happen, so this is good.

Starting in JDK 1.4b2, I get extraneous characters when I type alt-arrow.

This may not sound like a big deal, but there are other applications that
define alt-arrow, so that makes it an easy mistake to make. It may also be a
symptom of a more serious problem with event handling.

  To reproduce, run this program. Click into the JTextField, the JTextArea,
or the JEditorPane. Hold down the alt key and type any of the four arrow
keys. Under JDK 1.4b1 or earlier, nothing happens. Under JDK 1.4b2, strange
characters get inserted into the text.

*/

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.plaf.multi.*;
import javax.swing.plaf.basic.*;
import javax.swing.plaf.metal.*;

public class AltArrowBug extends JFrame
{
  public static void main(String[] args)
  {
    // Uncomment a pair of lines to test under a different look and feel.
//    try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel"); }
//    catch (Exception err) { }
//    try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
//    catch (Exception err) { }
    new AltArrowBug();
  }
  
  AltArrowBug()
  {
    super("Alt-Arrow Bug");
    addWindowListener(new WindowAdapter()
      { public void windowClosing(WindowEvent evt) { System.exit(0); } } );
    setBounds(20, 20, 400, 300);
    String jdkvers = System.getProperty("java.version");
    JTextField fld = new JTextField("This is a test. Java version = " +jdkvers);
    JTextArea area = new JTextArea("This is also a test. \nJava version = " +jdkvers);
    JScrollPane areaScr = new JScrollPane(area);
    JEditorPane editor = new JEditorPane();
    editor.setText("And this is a test. \nJava version = " + jdkvers);
    JScrollPane edScr = new JScrollPane(editor);
    Container cp = getContentPane();
    cp.setLayout(new BorderLayout());
    JPanel dualPanel = new JPanel(new GridLayout(0,1));
    cp.add(fld, "North");
    dualPanel.add(areaScr);
    dualPanel.add(edScr);
    cp.add(dualPanel, "Center");
    show();
  }
}
(Review ID: 131445) 
======================================================================

Name: ddT132432			Date: 11/29/2001


java version "1.4.0-beta3"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta3-b84)
Java HotSpot(TM) Client VM (build 1.4.0-beta3-b84, mixed mode)

In JTextField when I press ALT + PGUP appears "c".

---------------------------------------------------------------------------
import javax.swing.*;
import java.awt.event.*;

public class Test implements KeyListener{
    /**
     * Invoked when a key has been typed.
     * See the class description for {@link KeyEvent} for a definition of
     * a key typed event.
     */
    public void keyTyped(KeyEvent e) {
        System.out.println("key typed:"+e.getKeyChar ()+":"+e.getModifiers ());
    }
    /**
     * Invoked when a key has been pressed.
     * See the class description for {@link KeyEvent} for a definition of
     * a key pressed event.
     */
    public void keyPressed(KeyEvent e) {
        System.out.println("key pressed:"+e.getKeyChar ()+":"+e.getModifiers());
    }
    /**
     * Invoked when a key has been released.
     * See the class description for {@link KeyEvent} for a definition of
     * a key released event.
     */
    public void keyReleased(KeyEvent e) {
        System.out.println("key released:"+e.getKeyChar ()+":"+e.getModifiers());
    }

    public static void main(String [] args){
        JFrame frame=new JFrame("Test");
        JTextField field=new JTextField(20);
        field.addKeyListener (new Test());
        frame.getContentPane ().add(field);
        frame.pack();
        frame.setVisible(true);
    }
}
------------------------------------------------------------------------------

When I press the character "c", I become the output:

key pressed:c:0
key typed:c:0
key released:c:0

When I press ALT + PGUP, I become the output :

key pressed:?:8
key pressed:?:8
key released:?:8
key released:?:0
key typed:c:0
(Review ID: 136497)
======================================================================

Comments
EVALUATION Previous to this problem's introduction, a Swing text component's processKeyEvent was not sent a KeyEvent of type KEY_TYPED when these combinations of keys were typed. Currently, typing alt plus one of the arrow creates and sends a KEY_TYPED KeyEvent with a valid character (such as B, X, etc...). Swing processes them as any other KeyTyped. I beleive the problem is somewhere in AWT. Re-assigning. ###@###.### 2001-09-12 I tried this on WinNT with an AWT TextField. Spy++ reveals that when I press Alt - LeftArrow, I get a WM_CHAR message after both keys have been released. The result is a comma entered into the TextField. This WM_CHAR is not sent to the TextField under 1.3 or 1.3.1. ###@###.### 2001-09-16 Commit to fix in Tiger. ###@###.### 2001-09-17
16-09-2001