除手动解析外,Java中是否有任何默认方式可将KeyStroke转换为可读的String

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

Print result:
ctrl released 1

Expected:
Ctrl+1

最佳答案

来自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是错误的,毫无根据的说这不是默认值的一部分

10-06 13:40