可读的KeyStroke

时间:2014-11-26 14:06:00

标签: java swing keystroke

除了手动解析之外,Java是否有任何默认方式将KeyStroke翻译为可读String

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

Print result:
ctrl released 1

Expected:
Ctrl+1

1 个答案:

答案 0 :(得分:2)

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