FullScreen Swing Components Fail to Receive Keyboard Input on Java 7 on Mac OS X Mountain Lion

后端 未结 3 1728
野趣味
野趣味 2020-12-03 05:17

Update 12/21:

7u10 was recently released. Confirmed that:

  1. The issue still persists
  2. Thankfully, the workaround still function
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-03 05:47

    This is because the component to which you added the other has now lost focus, you can fix this by either:

    • calling requestFocus() on the component instance to which you add KeyBindings

    or

    • alternatively use JComponent.WHEN_IN_FOCUSED_WINDOW with KeyBindings:

      component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_Q, 0),
                              "doSomething");
      component.getActionMap().put("doSomething",
                               anAction);
      

    Reference:

    • How to Use Key Bindings

提交回复
热议问题