InputMap for multiple keys

I want to have a custom Tab and Shift + Tab in my Swing app. This works fine for JTextField textField

when pressing TAB =>

textField.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0), "Tab");

    textField.getActionMap().put("Tab", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            navigateDown();
        }
    });

      

But I want to have an implementation Shift + Tab

and I used this code: -

textField.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
KeyStroke.getKeyStroke(KeyEvent.VK_SHIFT, KeyEvent.VK_TAB), "BackTab");

    textField.getActionMap().put("BackTab", new AbstractAction() {
        public void actionPerformed(ActionEvent e) {
            navigateUp();
        }
    });

      

But that doesn't work for me. Thanks for your attention.

+3


source to share


1 answer


Wrong keystroke. The second integer is not a key code, but a modifier.

Try the following:



textField.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(
KeyStroke.getKeyStroke(KeyEvent.VK_TAB, java.awt.event.InputEvent.SHIFT_DOWN_MASK), "BackTab");

textField.getActionMap().put("BackTab", new AbstractAction() {
    public void actionPerformed(ActionEvent e) {
        navigateUp();
    }
});

      

See also JavaDoc at Keystroke.getKeyStroke()

+4


source







All Articles