Name: jl125535 Date: 02/07/2002
FULL PRODUCT VERSION :
java version "1.3.1"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1-b24)
Java HotSpot(TM) Client VM (build 1.3.1-b24, mixed mode)
FULL OPERATING SYSTEM VERSION :
winnt 4.0 SP 6
A DESCRIPTION OF THE PROBLEM :
A method should be addeded to javax.swing.KeyStroke. This
method should return a String that can be passed successfully
to javax.swing.KeyStroke.getKeyStroke(String s).
Although this can be done outside the core API I belive it
is a worthwhile addition for the following reasons:
1. it is easy to add
2. There are no backwards compatability issues
3. This would alleviate a lot of confusion about what to
pass to KeyStroke.getKeyStroke
4. This String returned could be stored in a property file
and then used later to recreate the KeyStroke. This would be
handy for customizing keyboard shortcuts.
The new method should perform a function similar to this
simplified example: (note that this is incomplete and will
not work for all key events)
// WARNING: partial workaround only
public static String getKeyStokeText(KeyStroke keyStroke) {
int code = keyStroke.getKeyCode();
int mod = keyStroke.getModifiers();
StringBuffer keyStrokeText = new StringBuffer()
.append(KeyEvent.getKeyModifiersText(mod).toLowerCase())
.append(' ')
.append(KeyEvent.getKeyText(code));
return keyStrokeText.toString();
}
This is similar to Bug #4488881 but doesn't break existing code.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Compile to sample program
2. Run java KeyReader
3. Press Control+x
EXPECTED VERSUS ACTUAL BEHAVIOR :
The method should return a string that can be passed to
KeyStroke.getKeyStroke. My example does this for some
keystrokes. I would like a full implementation to be added
to the KeyStroke class.
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
public class KeyReader extends JFrame implements KeyListener {
JTextField display = new JTextField();
public KeyReader() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
display.setEditable(false);
display.setBounds(new Rectangle(4, 116, 364, 36));
display.addKeyListener(this);
this.getContentPane().setLayout(null);
this.getContentPane().add(display, null);
addKeyListener(this);
setSize(400,400);
setVisible(true);
}
public static String getKeyStokeText(KeyStroke keyStroke) {
int code = keyStroke.getKeyCode();
int mod = keyStroke.getModifiers();
StringBuffer keyStrokeText = new StringBuffer()
.append(KeyEvent.getKeyModifiersText(mod).toLowerCase())
.append(' ')
.append(KeyEvent.getKeyText(code));
return keyStrokeText.toString();
}
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode();
int mod = e.getModifiers();
KeyStroke keyStroke = KeyStroke.getKeyStrokeForEvent(e);
//String keyStrokeText = keyStroke.getKeyStrokeText(); -- This is what I
would like to do.
String keyStrokeText = getKeyStokeText(keyStroke);
display.setText(keyStrokeText);
// load a new KeyStroke from the keyStrokeString
KeyStroke newKeyStroke = KeyStroke.getKeyStroke(keyStrokeText);
System.out.print(keyStrokeText);
System.out.print("==");
System.out.println(newKeyStroke);
}
public void keyReleased(KeyEvent e) {
}
public void keyTyped(KeyEvent e) {
}
public static void main(String[] args) {
try {
KeyReader kr = new KeyReader();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
---------- END SOURCE ----------
(Review ID: 137902)
======================================================================