-
Bug
-
Resolution: Cannot Reproduce
-
P3
-
None
-
1.3.0
-
generic
-
generic
Name: boT120536 Date: 05/20/2001
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0)
Java HotSpot(TM) Client VM (build 1.3.0, mixed mode)
Compile the single source file, attached.
Execute it.
A JFrame will display (300x300) with a sample Menubar.
It waits for key events to occur inside of it.
NOTE 2 RELATED(?) BUGS are reported:
Bug #1
------
Select the JFrame and press the MINUS key
located on the keyboard.
The program will display messages to stdout
describing the key event.
Bug #2
------
Select the menu bar; notice a single menu item
with an accelerator key set for the MINUS key.
The accelerator is set to "Ctrl+MINUS".
We expected it to be set to "Ctrl+-" and have
found no way to set the "-" character as the
accelerator string. The effort to do so resulted
in finding the Bug #1 listed above.
Notice that for KEY TYPED, key code is "Unknown"
for the MINUS key.
Source Code:
------------
// ------------------------------------------------------------
// KeyboardListener.java --- A generic KeyListener object.
// ------------------------------------------------------------
//
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
/**
* Implements and provides a default implementation for a KeyListener
interface.
*/
public class KeyboardListener implements KeyListener
{
/**
* Null constructor.
*/
public KeyboardListener()
{
}
//
// ----------------- KeyListener event handlers ---------------
// These handlers are the default implementation. Subclasses should
// override these methods if interested/necessary.
/** Handle a component-defined keyboard event (keyPressed) that has occured.
* @param KeyEvent the keystroke event data generated by the component object
* as a result of the user pressing a key within the component object.
*/
public void keyPressed(KeyEvent ke)
{
displayInfo(ke, "KEY PRESSED: ");
}
/** Handle a component-defined keyboard event (keyReleased) that has occured.
* @param KeyEvent the keystroke event data generated by the component object
* as a result of the user releasing a pressed key within the component
* object.
* @param KeyEvent the key event generated by the user w/ a key press.
*/
public void keyReleased(KeyEvent ke)
{
displayInfo(ke, "KEY RELEASED: ");
System.out.println("\n");
}
/** Handle a component-defined keyboard event (keyTyped) that has occured.
* @param KeyEvent the keystroke event data generated by the component object
* as a result of the user typing a key (press and then release) within the
* component object.
* @param KeyEvent the key event generated by the user w/ a key press.
*/
public void keyTyped(KeyEvent ke)
{
displayInfo(ke, "KEY TYPED: ");
}
/**
* Private helper method for debugging or verbose output.
* @param String a text desdcription of the event.
* @param AWTEvent a generic event.
*/
private void printMessage(String eventDescription, AWTEvent e)
{
System.out.println("\n" + eventDescription + e.toString());
}
/**
* Display some useful information about the KeyEvent.
* @param KeyEvent the key event generated by the user w/ a key press.
* @param String an information string to print to stdout.
*/
protected void displayInfo(KeyEvent e, String s)
{
String charString, keyCodeString, modString, tmpString;
String newline = "\n";
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)";
}
System.out.println(s + newline
+ " " + charString + newline
+ " " + keyCodeString + newline
+ " " + modString + newline);
}
//
// -----------------
// Main Test Driver
// -----------------
//
//
public static void main(String[] args)
{
JFrame frame = new JFrame("Testing key listener");
JMenuBar menuBar = new JMenuBar();
JMenu testMenu = new JMenu("Test");
JMenuItem menuItem = new JMenuItem("test item");
// Set menu item attributes
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_MINUS,
ActionEvent.CTRL_MASK));
// Finish up and display
testMenu.add(menuItem);
menuBar.add(testMenu);
frame.getRootPane().setMenuBar(menuBar);
frame.addKeyListener(new KeyboardListener());
frame.setSize(300,300);
frame.setVisible(true);
}
}
(Review ID: 124695)
======================================================================