Application wide keyboard shortcut - Java Swing

后端 未结 6 811
[愿得一人]
[愿得一人] 2020-11-30 01:58

I would like to create an application wide keyboard shortcut for a Java Swing application. Looping over all components and adding the shortcut on each, has focus related sid

6条回答
  •  半阙折子戏
    2020-11-30 02:25

    A little simplified example:

    KeyboardFocusManager keyManager;
    
    keyManager=KeyboardFocusManager.getCurrentKeyboardFocusManager();
    keyManager.addKeyEventDispatcher(new KeyEventDispatcher() {
    
      @Override
      public boolean dispatchKeyEvent(KeyEvent e) {
        if(e.getID()==KeyEvent.KEY_PRESSED && e.getKeyCode()==27){
          System.out.println("Esc");
          return true;
        }
        return false;
      }
    
    });
    

提交回复
热议问题