1

I tried the usual way to handle keypress: implements KeyListener and override keyPressed(). But there are many swing components, and if Im on a combobox, the combobox "thinks" the key is for him. I want to the main app capture the keypress, how to?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
John Smith
  • 6,129
  • 12
  • 68
  • 123
  • u mean adding the same behaviour to all components? for example when u press right arrow to transfer focus?? – nachokk Jun 11 '13 at 21:58
  • something like that :) to capture all keypressed event, no matter if any component is in focus – John Smith Jun 11 '13 at 22:07
  • See [this Java trail](http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html) – Brian Jun 11 '13 at 22:08
  • I found the easiest solution: http://stackoverflow.com/questions/12434740/how-do-you-make-key-bindings-for-a-java-awt-frame – John Smith Jun 11 '13 at 22:39

3 Answers3

4

You are looking for this Key Bindings

nachokk
  • 14,363
  • 4
  • 24
  • 53
  • Yes, the solution will be related to key bindings. But what if he has a `JTextField` that has focus and you want to globally react to Ctrl+C? That tutorial doesn't help. See my answer below for how to do things like that. – Daniel Kaplan Jun 11 '13 at 22:27
3

You could use:

KeyEventPostProcessor kepp = new KeyEventPostProcessor() {
  @Override 
  boolean postProcessKeyEvent(KeyEvent e) {
     // handle key event globally
  }
};

KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
kfm.addKeyEventPostProcessor(kepp);
Tilo
  • 3,255
  • 26
  • 31
  • "cooperates with the current KeyboardFocusManager in the final resolution of all **unconsumed** KeyEvents." May not be useful for his needs. – Daniel Kaplan Jun 11 '13 at 22:22
  • The Javadoc is a bit misleading. The important sentence is: "Typically, KeyEvent post-processing will be used to implement features which require global KeyEvent post-handling, such as menu shortcuts." – Tilo Jun 11 '13 at 22:25
  • Those two statements don't necessarily contradict. How is it misleading? – Daniel Kaplan Jun 11 '13 at 22:29
  • It is misleading in my opinion because the sentence you quoted sounds like it only receives KeyEvents which have not been consumed by a JComponent. This is however not the case. – Tilo Jun 11 '13 at 23:22
  • I've done a test that shows you're right. I don't understand. That documentation isn't so much misleading as it is **wrong**. – Daniel Kaplan Jun 11 '13 at 23:29
1

This is way harder than you'd think. For example, here's the effort you have to put in to get this working on a Panel with a JFileChooser in it. That's a similar question I asked in the past. I think that will help you solve your problem. You should read both answers given as they're both valid.

To summarize, you've gotta iterate all the InputMaps and the parents of the InputMaps and clear out the keys you want to globally use. Then apply your action to the KeyMap.

You'll need this:

private static void clearInputMap(InputMap inputMap) {
    inputMap.clear();
    while ((inputMap = inputMap.getParent()) != null) {
        inputMap.clear();
    }
}
Community
  • 1
  • 1
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356