简体   繁体   English

如何在所有组件中监听按键(在Java Swing中)?

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

I would like to listen for key combinations such as Control + S without adding key listeners to each component in my Swing application. 我想听一些关键组合,比如Control + S,而不是在我的Swing应用程序中为每个组件添加关键监听器。 How can I achieve this? 我怎样才能做到这一点?

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. 返回false允许键盘焦点管理器恢复正常的键事件调度到各种组件。

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. 释放钥匙后,将其从设备中取出。

Very simple my friend: All you have to do is create a class KeyEventDispatcher and register to KeyboardFocusManager C.1 非常简单我的朋友:你所要做的就是创建一个KeyEventDispatcher类并注册到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(); 然后解析并提取状态和密钥信息,参见:C.2奇怪的是,你必须通过ThatEvent.getID()获得KEY STATE;

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;
  }
}

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. 这应该只需要做一次,所以我想你可以把它完成并继续前进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM