Mitch Weaver Mitch Weaver - 6 months ago 11
Java Question

Adding TAB key java swing keybindings --- What is the correct name?

Action tab = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
System.err.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
}
};

Game.panel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("Tab"), "tab");
Game.panel.getActionMap().put("tab", tab);


However its not printing. This is the same set up that I use for all my other keys, but I can't get the tab key to work. I've tried
"TAB"
,
"Tab"
, and
"tab"
.

What's the correct name for it? Or is there something special?

Answer

You probably want:

KeyStroke.getKeyStroke('\t');

\t is the escape sequence for the TAB character (0x09), which is output by pressing tab on your keyboard.

There is also KeyEvent.VK_TAB which might be preferable depending on your use case. See the documentation in KeyEvent for more on why you should prefer one or the other.

Comments