获取"<"和">"的KeyStroke

Rag*_*son 4 java swing

我想对<&>keys 使用keybinding ,然后在我的JFrame上使用它.

我正在使用以下代码尝试获取<key.

KeyStroke testStroke = KeyStroke.getKeyStroke("<"); 
mainJFrame.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
                        .put(testStroke, "clickButton");
mainJFrame.getRootPane().getActionMap().put("clickButton", new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("PRESS!!!!");
        }
});
Run Code Online (Sandbox Code Playgroud)

我无法让它发挥作用.但是,如果我使用像A这样的键,它会很好用

KeyStroke testStroke = KeyStroke.getKeyStroke("A"); 
Run Code Online (Sandbox Code Playgroud)

所以我认为KeyStroke是错误的,其余的代码都没问题.

如何获得按键<&>的按键?

Anu*_*oob 5

根据该文件getKeyStroke(char):

返回KeyStroke的共享实例,该实例表示指定字符的KEY_TYPED事件.

KeyStroke.getKeyStroke('<');
KeyStroke.getKeyStroke('>');
Run Code Online (Sandbox Code Playgroud)

之前,您使用的是String.寻找和文档getKeyStroke(java.lang.String):

解析字符串并返回KeyStroke.该字符串必须具有以下语法:

该方法具有复杂的语法.单个字母有效,但特殊字符不遵循语法.这getKeyStroke(char)简单得多.你应该使用它.