How can I listen for key presses (within Java Swing) across all components?

前端 未结 3 1777
傲寒
傲寒 2020-11-28 08:31

I would like to listen for key combinations such as Control+S without adding key listeners to each component in my Swing application. How can I achieve

相关标签:
3条回答
  • 2020-11-28 09:14

    I don't think there is a way to add a "global" key listener like you are wanting to do. This forum post with a similar question backs me up. You are just going to have to add them to each component. This should only need to be done once though, so I guess you can just get it over with and move on.

    0 讨论(0)
  • Very simple my friend: All you have to do is create a class KeyEventDispatcher and register to KeyboardFocusManager C.1

    Then parse and extract state and key info see: C.2 Strangely enough though, you have to get the KEY STATE via ThatEvent.getID();

    SEEMS LIKE A MISSNOMER TO ME.

    ///////////////////////////////////////////////////////////////////////////////////////////   C.1)
             KeyDispatcher        ThisKeyDispatcher  = new KeyDispatcher();
    
             KeyboardFocusManager ThisKbFocusMngr = KeyboardFocusManager 
                                                  . getCurrentKeyboardFocusManager();
    
             ThisKbFocusMngr                      . addKeyEventDispatcher(ThisKeyDispatcher);
             return ThisKeyDispatcher;
    
    ///////////////////////////////////////////////////////////////////////////////////////////   
    C.2
    public static class KeyDispatcher implements KeyEventDispatcher {
    
      public boolean dispatchKeyEvent(final KeyEvent ThatEvent) {
    
         KeyboardFocusManager ThisKbFocusMngr    = null;
         Component            ThisComponent      = null;
         Container            ThisRoot           = null;
         Window               ThisWindow         = null;
         int                  ThisKeyStateEvent  = 0;
    
         try {
            ThisKbFocusMngr       = KeyboardFocusManager  . getCurrentKeyboardFocusManager();
            ThisComponent         = ThisKbFocusMngr       . getFocusOwner();
            ThisRoot              = ThisKbFocusMngr       . getCurrentFocusCycleRoot();
            ThisWindow            = ThisKbFocusMngr       . getActiveWindow();
            ThisKeyStateEvent     = ThatEvent.getID();   // i.e.  KeyEvent.KEY_RELEASED
    
            if(false                           == ThatEvent.isConsumed()) {
               boolean       RetBool          = false;
               if((KeyEvent.VK_BACK           == ThatEvent.getKeyCode())) {
                  RetBool                      = true;
               } else {
                  RetBool                      = m_CallSomeoneEvent(ThatEvent);
               }
               if(RetBool) {
                  ThatEvent.consume();
                  return true;
               }
            }
         }
         catch( Throwable e ) {
            LogThis(". ", e);
         }
         return false;
      }
    }
    
    0 讨论(0)
  • 2020-11-28 09:34

    It is possible.

    KeyboardFocusManager.getCurrentKeyboardFocusManager()
      .addKeyEventDispatcher(new KeyEventDispatcher() {
          @Override
          public boolean dispatchKeyEvent(KeyEvent e) {
            System.out.println("Got key event!");
            return false;
          }
    });
    

    That will grab all key events. Returning false allows the keyboard focus manager to resume normal key event dispatching to the various components.

    If you want to catch key combos, you can keep a set of "pressed keys." Whenever a key is pressed, add it to the set and check what keys are already in the set. When a key is released, remove it from the set.

    0 讨论(0)
提交回复
热议问题