Java-是否检测到带有KeyBindings的按键? [英] Java - Detecting any key press with KeyBindings?

查看:186
本文介绍了Java-是否检测到带有KeyBindings的按键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读到,使用KeyBindings比使用KeyListeners更好.我看到了KeyBindings如何对特定的键做出特定的反应.但是我也在尝试检测键盘上的ANY键的按下/释放:是否可以通过KeyBindings来做到这一点?

I read that it is better to use KeyBindings than KeyListeners. I see how KeyBindings are useful for a specific reaction to a specific key; but I am also trying to detect the press/release of ANY key on the keyboard: is there a way to do this with KeyBindings?

例如,我通常会使用KeyBindings这样对单个键进行操作:

For example, I would use KeyBindings normally to act on a single key as so:

InputMap iMap = component.getInputMap();
ActionMap aMap = component.getActionMap();

iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "enter without modifiers");
aMap.put("enter without modifiers", new AbstractAction(){
      public void actionPerformed(ActionEvent a){
              System.out.println("Enter pressed alone");
      });

因此,我正在考虑使用这种方法来检测任何按键:

So I am thinking something like this for detecting any key press:

iMap.put(KeyStroke.getKeyStroke(KeyEvent.KEY_PRESSED, 0), "any key was pressed");
aMap.put("any key was pressed", new AbstractAction(){
      public void actionPerformed(ActionEvent a){
              System.out.println("some key was pressed, regardless of which key...");
      });

有没有办法做到这一点?

Is there a way to accomplish this?

还有,有没有办法用ANY修饰符组合来捕获它们的KeyBinding?例如.是否映射了Enter-action,而不管是否保留了修饰符,或者是否同时按住了ctrl-alt等组合在一起?

Also, is there a way to catch they KeyBinding with ANY modifiers combination? E.g. to have the Enter-action mapped regardless of if no modifiers are held, or if both ctrl-alt etc. in any combination are held?

非常感谢, 丹

交叉发布于: http://www.javaprogrammingforums.com/whats-wrong-my-code/26194-how-detect-any-key-press-keybindings.html#post103862

推荐答案

我看到了KeyBindings如何对特定的键做出特定的反应;

I see how KeyBindings are useful for a specific reaction to a specific key;

是的,那就是您要使用按键绑定

Yes, that is when you would use key bindings

但是我还试图检测键盘上的ANY键的按下/释放:是否可以使用KeyBindings来做到这一点?

but I am also trying to detect the press/release of ANY key on the keyboard: is there a way to do this with KeyBindings?

不,键绑定不是用于此目的.

No, key bindings is not used for this purpose.

还有,有没有办法通过ANY修饰符组合来捕获它们的KeyBinding?

Also, is there a way to catch they KeyBinding with ANY modifiers combination?

否,同样,绑定是针对特定的KeyStroke的.因此,您将需要编写一种方法来为每个组合添加绑定.请注意,顺序无关紧要.那就是Shift + Alt与Alt + Shift相同

No, again bindings are for a specific KeyStroke. So you would need to write a method to add bindings for each combination. Note that order doesn't matter. That is Shift+Alt is the same as Alt+Shift

在大多数情况下,键绑定是首选方法.在您的情况下不是.

In most cases Key Bindings is the preferred approach. In your case it is not.

如果您正在侦听具有焦点的特定组件上的KeyEvent,则KeyListener可能是合适的.

A KeyListener may be appropriate if you are listening for KeyEvents on a specific component that has focus.

或者,如果您想更广泛地听取KeyEvent,则可以查看全局事件调度,具体取决于您的要求.

Or it you want to listen for KeyEvents on a more global basis then you can take a look at Global Event Listeners or maybe Global Event Dispatching depending on your exact requirement.

这篇关于Java-是否检测到带有KeyBindings的按键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆