Depending on what you want to achieve, you could use the Key Bindings API, for example...
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap am = getActionMap();
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0), "Press.A");
am.put("Press.A", new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
gameConsole.append("\n\nCommands: \n ==========");
commands();
}
});
Now the great thing about this is, you can reuse the Action
...
For example...
public class ConsoleAction extends AbstractAction {
public ConsoleAction() {
putValue(NAME, "Text of button");
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_A, 0));
putValue(MNEMONIC_KEY, KeyEvent.VK_A);
}
public void actionPerformed(ActionEvent e) {
gameConsole.append("\n\nCommands: \n ==========");
commands();
}
}
And then...
ConsoleAction consoleAction = new ConsoleAction();
JButton consoleButton = new JButton(consoleAction);
//...
am.put(consoleAction);