can one disable key bindings on a JComponent?
Suppose that I've got a set of keys bound to a JComponent. Something like this:
Code:
public void bindKeys() {
// variable declarations:
InputMap inputMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = getActionMap();
int PRESSED = InputProcessor.PRESSED;
int RELEASED = InputProcessor.RELEASED;
int UP = KeyEvent.VK_UP;
int RIGHT = KeyEvent.VK_RIGHT;
int DOWN = KeyEvent.VK_DOWN;
int LEFT = KeyEvent.VK_LEFT;
int N = KeyEvent.VK_N; // new maze
int R = KeyEvent.VK_R; // restart level
int Q = KeyEvent.VK_Q; // quit
int P = KeyEvent.VK_P; // pause
// up arrow key
inputMap.put(KeyStroke.getKeyStroke(UP, 0, false), "up_pressed");
actionMap.put("up_pressed", new InputProcessor(PRESSED, UP));
inputMap.put(KeyStroke.getKeyStroke(UP, 0, true), "up_released");
actionMap.put("up_released", new InputProcessor(RELEASED, UP));
// right arrow key
inputMap.put(KeyStroke.getKeyStroke(RIGHT, 0, false), "right_pressed");
actionMap.put("right_pressed", new InputProcessor(PRESSED, RIGHT));
inputMap.put(KeyStroke.getKeyStroke(RIGHT, 0, true), "right_released");
actionMap.put("right_released", new InputProcessor(RELEASED, RIGHT));
// down arrow key
inputMap.put(KeyStroke.getKeyStroke(DOWN, 0, false), "down_pressed");
actionMap.put("down_pressed", new InputProcessor(PRESSED, DOWN));
inputMap.put(KeyStroke.getKeyStroke(DOWN, 0, true), "down_released");
actionMap.put("down_released", new InputProcessor(RELEASED, DOWN));
// left arrow key
inputMap.put(KeyStroke.getKeyStroke(LEFT, 0, false), "left_pressed");
actionMap.put("left_pressed", new InputProcessor(PRESSED, LEFT));
inputMap.put(KeyStroke.getKeyStroke(LEFT, 0, true), "left_released");
actionMap.put("left_released", new InputProcessor(RELEASED, LEFT));
// 'n' (for new maze)
inputMap.put(KeyStroke.getKeyStroke(N, 0, false), "n_pressed");
actionMap.put("n_pressed", new InputProcessor(PRESSED, N));
// 'r' (for restart level)
inputMap.put(KeyStroke.getKeyStroke(R, 0, false), "r_pressed");
actionMap.put("r_pressed", new InputProcessor(PRESSED, R));
// 'q' (for quit)
inputMap.put(KeyStroke.getKeyStroke(Q, 0, false), "q_pressed");
actionMap.put("q_pressed", new InputProcessor(PRESSED, Q));
// 'p' (for pause)
inputMap.put(KeyStroke.getKeyStroke(P, 0, false), "p_pressed");
actionMap.put("p_pressed", new InputProcessor(PRESSED, P));
}
Is there a simple way to temporarily disable the key bindings? Something like
Code:
myComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).setEnabled(false);
I know about the InputMap.remove(KeyStroke) but I'm wondering if there's something simpler (and something that doesn't involve actually removing the key bindings) like the hypothetical setEnabled(boolean) method above.