Readable KeyStroke

272 views Asked by At

Is there any default way in Java to translate KeyStroke to readable String except manual parsing?

KeyStroke ke = KeyStroke.getKeyStroke("ctrl released 1");
System.out.println(ke.toString())

Print result:
ctrl released 1

Expected:
Ctrl+1
1

There are 1 answers

0
SeniorJD On BEST ANSWER

From Java Swing API:

KeyStroke accelerator =  b.getAccelerator();
String acceleratorText = "";
if (accelerator != null) {
    int modifiers = accelerator.getModifiers();
    if (modifiers > 0) {
        acceleratorText = KeyEvent.getKeyModifiersText(modifiers);
        acceleratorText += "+";
    }
    acceleratorText += KeyEvent.getKeyText(accelerator.getKeyCode());
}

@mKorbel, U're wrong and groundless saying that it isn't a part of defaults