0

I'm going to make a hidden dialog in my application that get visible with a keyboard combination (e.g. Five sequent Ctrl+Shift+i).
How Can I capture keyboard combination strokes globally on the entire application?

Thanks

vahid abdi
  • 9,636
  • 4
  • 29
  • 35
Ariyan
  • 14,760
  • 31
  • 112
  • 175
  • 1
    You do know that a lot of keyboards will choke on more than two modifier keys being pressed simultaneously, right? – thkala Apr 14 '12 at 16:47
  • @thkala: No I don't! I never seen such a situation! but the count of modifiers is not important for me! I need only a combinations that be pressed number of sequent times (Even five Ctrl+a is acceptable!) – Ariyan Apr 14 '12 at 17:01

2 Answers2

2

FullScreenTest is an example that shows how to use Action and Key Bindings in this context. You could substitute KeyEvent.VK_I and the relevant KeyStroke modifiers. Your action listener can keep count of how often it's been triggered.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
1

I solved it by defining a DispatcherListener:

class DispatcherListener implements KeyEventDispatcher{
    private int level=0;

    public boolean dispatchKeyEvent(KeyEvent e) {
        if(e.getID() == KeyEvent.KEY_RELEASED){
            if(e.isControlDown() && e.isShiftDown()){
                if(this.level==0 && e.getKeyCode()==KeyEvent.VK_S){
                    level++;
                }else if(this.level==1 && e.getKeyCode()==KeyEvent.VK_H){
                    level++;
                }else if(this.level==2 && e.getKeyCode()==KeyEvent.VK_O){
                    level++;
                }else if(this.level==3 && e.getKeyCode()==KeyEvent.VK_W){
                    level=0;
                    this.showHiddenWindow((JFrame)SwingUtilities.getRoot(e.getComponent()));
                }else{
                    level=0;
                }

                //System.out.println( "level: " +  level );
            }
        }
        return false;
    }

and used it as this:

    KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    manager.addKeyEventDispatcher( new DispatcherListener());

Thank you all

Ariyan
  • 14,760
  • 31
  • 112
  • 175