JDK-4634696 : Add a method to KeyStroke to get a String that is compatible with getKeyStroke
  • Type: Enhancement
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 1.4.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_nt
  • CPU: x86
  • Submitted: 2002-02-07
  • Updated: 2003-04-24
  • Resolved: 2003-04-24
Related Reports
Duplicate :  
Description

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) 
======================================================================

Comments
EVALUATION As the majority of the code has been promoted to java.awt.AWTKeyStroke, I am reassinging to awt. ###@###.### 2002-02-07 Most of the docs for AWT and Swing classes that implement toString say that the return value can change between implementations. As well they should: if we add new fields to these classes, the new fields should be added to the output of toString() as well. We are planning to fix this rfe in AWT and Swing under 4370733. ###@###.### 2003-04-23
23-04-2003