Reputation: 1314
Small single-window application (game) has a graphical object controlled by GUI buttons. I have a set of keyboard shortcuts mapped to them (i. e., arrow keys). Is it reasonably easy to have an option to change the set of shortcuts on the fly? For instance, JOption to select between arrow keys and WASD?
While I am still struggling with the binding, here is the idea I have with the switch itself:
// KeyStroke objects to be used when mapping them to the action
KeyStroke keyUp, keyLeft, keyRight, keyDown;
JRadioButton[] kbdOption = new JRadioButton[2];
kbdOption[0] = new JRadioButton("Arrow Keys");
kbdOption[1] = new JRadioButton("WASD");
if (kbdOption[0].isSelected()) {
keyUp = KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0);
keyLeft = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0);
keyRight = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0);
keyDown = KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0);
} else if (kbdOption[0].isSelected()) {
keyUp = KeyStroke.getKeyStroke(KeyEvent.VK_W, 0);
keyLeft = KeyStroke.getKeyStroke(KeyEvent.VK_A, 0);
keyRight = KeyStroke.getKeyStroke(KeyEvent.VK_D, 0);
keyDown = KeyStroke.getKeyStroke(KeyEvent.VK_S, 0);
}
As I cannot test it myself, does it look decent? Is the scope proper (that is, can I actually use it in the same method that builds the rest of GUI or should if-else be called from elsewhere)? Shall it work on the fly instantly changing bindings as the program is running?
Upvotes: 1
Views: 166
Reputation: 44318
I'm inclined to go with Alex Stybaev's first response: just add all the bindings. Something like this:
gamePanel.getActionMap().put("left", leftAction);
gamePanel.getActionMap().put("right", rightAction);
gamePanel.getActionMap().put("up", upAction);
gamePanel.getActionMap().put("down", downAction);
InputMap inputMap =
gamePanel.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(KeyStroke.getKeyStroke("LEFT"), "left");
inputMap.put(KeyStroke.getKeyStroke("RIGHT"), "right");
inputMap.put(KeyStroke.getKeyStroke("UP"), "up");
inputMap.put(KeyStroke.getKeyStroke("DOWN"), "down");
inputMap.put(KeyStroke.getKeyStroke("A"), "left");
inputMap.put(KeyStroke.getKeyStroke("D"), "right");
inputMap.put(KeyStroke.getKeyStroke("W"), "up");
inputMap.put(KeyStroke.getKeyStroke("S"), "down");
inputMap.put(KeyStroke.getKeyStroke("KP_LEFT"), "left");
inputMap.put(KeyStroke.getKeyStroke("KP_RIGHT"), "right");
inputMap.put(KeyStroke.getKeyStroke("KP_UP"), "up");
inputMap.put(KeyStroke.getKeyStroke("KP_DOWN"), "down");
inputMap.put(KeyStroke.getKeyStroke("NUMPAD4"), "left");
inputMap.put(KeyStroke.getKeyStroke("NUMPAD6"), "right");
inputMap.put(KeyStroke.getKeyStroke("NUMPAD8"), "up");
inputMap.put(KeyStroke.getKeyStroke("NUMPAD2"), "down");
Upvotes: 2