" I would like to use keybinding for keys , and then use this on my JFrame. I use the following code to tr...">

Get KeyStroke for "<" and ">"

I would like to use keybinding for keys <and >, and then use this on my JFrame.

I use the following code to try to get it for the <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!!!!");
        }
});

I can not make it work. However, it works well if I use a key like A

KeyStroke testStroke = KeyStroke.getKeyStroke("A"); 

So, I think KeyStroke is wrong, and the rest of the code is fine.

How to get a keystroke for the <and> keys?

+4
source share
1 answer

According to the documentation for getKeyStroke(char):

Returns a generic KeyStroke instance that represents the KEY_TYPED event for the specified character.

KeyStroke.getKeyStroke('<');
KeyStroke.getKeyStroke('>');

String. getKeyStroke(java.lang.String):

KeyStroke. :

. , . getKeyStroke(char) . .

+5

Source: https://habr.com/ru/post/1544767/


All Articles