JDK-4200735 : For characters typed (e.g. ABCDE....) no keyTyped event is generated
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.swing
  • Affected Version: 1.1.6,1.2.0
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic,windows_nt
  • CPU: generic,x86
  • Submitted: 1999-01-05
  • Updated: 1999-04-02
  • Resolved: 1999-04-02
Related Reports
Duplicate :  
Description

Name: dbT83986			Date: 01/05/99


Type in alphabets in the text box
No keyTyped events generated for alphabets
keyTyped events being generated for backspace key though.
Tested on NT

import javax.swing.*;

import java.awt.BorderLayout;
import java.awt.Dimension;

import java.awt.event.*;

public class KeyEventDemo extends JApplet 
                          implements KeyListener,
				     ActionListener {
    JTextArea displayArea;
    JTextField typingArea;
    String newline;

    public void init() {
	JButton button = new JButton("Clear");
	button.addActionListener(this);

	typingArea = new JTextField(20);
	typingArea.addKeyListener(this);

	displayArea = new JTextArea();
	displayArea.setEditable(false);
	JScrollPane scrollPane = new JScrollPane(displayArea);
	scrollPane.setPreferredSize(new Dimension(375, 125));

	JPanel contentPane = new JPanel();
	contentPane.setLayout(new BorderLayout());
	contentPane.add(typingArea, BorderLayout.NORTH);
	contentPane.add(scrollPane, BorderLayout.CENTER);
	contentPane.add(button, BorderLayout.SOUTH);
	setContentPane(contentPane);

	newline = System.getProperty("line.separator");
    }

    /** Handle the key typed event from the text field. */
    public void keyTyped(KeyEvent e) {
	displayInfo(e, "KEY TYPED: ");
    }

    /** Handle the key pressed event from the text field. */
    public void keyPressed(KeyEvent e) {
	displayInfo(e, "KEY PRESSED: ");
    }

    /** Handle the key released event from the text field. */
    public void keyReleased(KeyEvent e) {
	displayInfo(e, "KEY RELEASED: ");
    }

    /** Handle the button click. */
    public void actionPerformed(ActionEvent e) {
	//Clear the text components.
	displayArea.setText("");
	typingArea.setText("");

	//Return the focus to the typing area.
	typingArea.requestFocus();
    }

    /*
     * We have to jump through some hoops to avoid
     * trying to print non-printing characters 
     * such as Shift.  (Not only do they not print, 
     * but if you put them in a String, the characters
     * afterward won't show up in the text area.)
     */
    protected void displayInfo(KeyEvent e, String s){
	String charString, keyCodeString, modString, tmpString;

	char c = e.getKeyChar();
	int keyCode = e.getKeyCode();
	int modifiers = e.getModifiers();

	if (Character.isISOControl(c)) {
	    charString = "key character = (an unprintable control character)";
	} else {
	    charString = "key character = '" + c + "'";
	}

	keyCodeString = "key code = " + keyCode
			+ " ("
			+ KeyEvent.getKeyText(keyCode)
			+ ")";

	modString = "modifiers = " + modifiers;
	tmpString = KeyEvent.getKeyModifiersText(modifiers);
	if (tmpString.length() > 0) {
	    modString += " (" + tmpString + ")";
	} else {
	    modString += " (no modifiers)";
	}

	displayArea.append(s
                           + newline + "    "
	                   + charString 
	                   + newline + "    "
		           + keyCodeString
	                   + newline + "    "
		           + modString
		           + newline);
    }
}
(Review ID: 48902)
======================================================================

Comments
EVALUATION This bug seems to be specific to Swing. I added System.out.println statements to the KeyEvent methods in the test case, and the pressed and released methods executed, but the typed method never did. I then replaced the Swing components with AWT components (necessitating a slight rewrite - my code is attached), and the keyTyped method worked just fine. I therefore concluded that this bug was specific to Swing. Note: the submitter originally filed this bug against "generic" hardware and OS. I changed it to win32 only, since the bug does not appear on Solaris. eric.hawkes@eng 1999-01-12 I can't reproduce this on NT with the current 1.2 workspace using the following slight modification. import javax.swing.*; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.event.*; public class bug4200735 extends JFrame implements KeyListener, ActionListener { JTextArea displayArea; JTextField typingArea; String newline; public static void main(String[] args) { bug4200735 claim = new bug4200735(); claim.init(); claim.setVisible(true); } public void init() { JButton button = new JButton("Clear"); button.addActionListener(this); typingArea = new JTextField(20); typingArea.addKeyListener(this); displayArea = new JTextArea(); displayArea.setEditable(false); JScrollPane scrollPane = new JScrollPane(displayArea); scrollPane.setPreferredSize(new Dimension(375, 125)); JPanel contentPane = new JPanel(); contentPane.setLayout(new BorderLayout()); contentPane.add(typingArea, BorderLayout.NORTH); contentPane.add(scrollPane, BorderLayout.CENTER); contentPane.add(button, BorderLayout.SOUTH); setContentPane(contentPane); newline = System.getProperty("line.separator"); } /** Handle the key typed event from the text field. */ public void keyTyped(KeyEvent e) { displayInfo(e, "KEY TYPED: "); } /** Handle the key pressed event from the text field. */ public void keyPressed(KeyEvent e) { displayInfo(e, "KEY PRESSED: "); } /** Handle the key released event from the text field. */ public void keyReleased(KeyEvent e) { displayInfo(e, "KEY RELEASED: "); } /** Handle the button click. */ public void actionPerformed(ActionEvent e) { //Clear the text components. displayArea.setText(""); typingArea.setText(""); //Return the focus to the typing area. typingArea.requestFocus(); } /* * We have to jump through some hoops to avoid * trying to print non-printing characters * such as Shift. (Not only do they not print, * but if you put them in a String, the characters * afterward won't show up in the text area.) */ protected void displayInfo(KeyEvent e, String s){ String charString, keyCodeString, modString, tmpString; char c = e.getKeyChar(); int keyCode = e.getKeyCode(); int modifiers = e.getModifiers(); if (Character.isISOControl(c)) { charString = "key character = (an unprintable control character)"; } else { charString = "key character = '" + c + "'"; } keyCodeString = "key code = " + keyCode + " (" + KeyEvent.getKeyText(keyCode) + ")"; modString = "modifiers = " + modifiers; tmpString = KeyEvent.getKeyModifiersText(modifiers); if (tmpString.length() > 0) { modString += " (" + tmpString + ")"; } else { modString += " (no modifiers)"; } displayArea.append(s + newline + " " + charString + newline + " " + keyCodeString + newline + " " + modString + newline); } } timothy.prinzing@eng 1999-03-31 I compiled and ran the above test case on NT4.0 SP3 using the latest promoted build, JDK1.2.2-M. KeyTyped events were not generated, so I reopened the bug. I also raised the priority, since this bug is impeding the Printing Enhancement work for Kestrel. eric.hawkes@eng 1999-04-01 This is caused by enabling input method, and is a known bug. timothy.prinzing@eng 1999-04-01
01-04-1999