Java中字符的键组合(非Ctrl或Alt)

时间:2011-12-11 14:32:10

标签: java swing key-bindings keystrokes key-events

我正在做一个需要检测某个键组合是否被按下的项目。任何键,如 Ctrl Alt A - Z 0 - 9 可用作组合键。

我使用KeyStroke.getKeyStroke搜索了一些代码,但似乎没有 Ctrl Shift Alt 等等。

我的应用程序需要检测组合,即使没有 Ctrl Alt ,例如只是普通 A + B + C 的组合。这也可以用KeyStroke.getKeyStroke来完成吗?回复非常感谢。

4 个答案:

答案 0 :(得分:1)

取决于应用程序是否具有GUI类型,对于GUI重新连接,您必须查找

DocumentListenerKeyBindings,我建议拒绝实现KeyListener以获取更复杂的代码和Focus解决方法

或添加addAWTEventListener(AWTEventListener listener, long eventMask),也可能会侦听KeyEvents和MouseEvents

答案 1 :(得分:1)

AFAIK,无法获取密钥的状态,这完全不同于:

KeyStroke keyStroke = KeyStroke.getKeyStroke("A");
// NOT supported
if (keyStroke.isPressed()) {
  // do something
}

唯一支持的是关于更改单个密钥的状态f.i的通知。在KeyListener的最低级别

public void keyPressed(KeyEvent e) {
    if (KeyEvent.VK_A == e.getKeyCode()) {
        ....
    }
}

要处理组合键(使用特殊修改键除外),无法跟踪已按下/释放哪些键以了解哪个 处于按下状态。

以下是在keyBindings级别上执行此操作的示例。基本成分

  • 触发器:包含执行最终动作的累积“武装”逻辑的实体
  • 解除触发器的动作
  • 用于释放/按下特定键击的键绑定,绑定到相应的布防操作

一些代码:

// the logic container
public static interface Trigger {
    public void addTrigger(String trigger);
    public void arm(ActionEvent e);
    public void disarm(ActionEvent e);
}

// a particular implementation which uses the actionCommand 
// as identifiers 
public static class ActionTrigger implements Trigger {
    private Action triggered;
    private List<String> triggers = new ArrayList<>();
    private List<String> armed = new ArrayList<>();

    public ActionTrigger(Action triggered) {
        this.triggered = triggered;
    }

    @Override
    public void arm(ActionEvent e) {
        String command = e.getActionCommand();
        if (!triggers.remove(command)) return;
        armed.add(command);
        if (triggers.isEmpty()) {
            triggered.actionPerformed(e);
        }
    }

    @Override
    public void disarm(ActionEvent e) {
        String command = e.getActionCommand();
        if (!armed.remove(command)) return;
        triggers.add(command);
    }

    @Override
    public void addTrigger(String trigger) {
        triggers.add(trigger);
    }
}

// an Action notifying the trigger of dis/arms
public static class ArmingAction extends AbstractAction {
    private Trigger trigger;
    private boolean arm;

    /**
     * @param trigger
     */
    public ArmingAction(Trigger trigger, String command, boolean arm) {
        this.trigger = trigger;
        this.arm = arm;
        putValue(ACTION_COMMAND_KEY, command);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (arm) {
            trigger.arm(e);
        } else {
            trigger.disarm(e);
        }
    }
}

// usage
// the action to trigger with multiple keys
Action action = new AbstractAction("real") {

    @Override
    public void actionPerformed(ActionEvent e) {
        LOG.info("******triggered: " + e);
    }

};

JComponent comp = new JPanel();
ActionMap actionMap = comp.getActionMap();
InputMap inputMap = comp.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
Trigger trigger = new ActionTrigger(action);
// the key combination
char[] chars = {'A', 'S', 'D'};
for (int i = 0; i < chars.length; i++) {
    // the identifier
    String command = "step" + chars[i];
    trigger.addTrigger(command);
    // binding for pressed
    String pressedID = "pressed" + chars[i];
    actionMap.put(pressedID, new ArmingAction(trigger, command, true));
    inputMap.put(KeyStroke.getKeyStroke("pressed " + chars[i]), pressedID);
    // binding for released
    String releasedID = "released" + chars[i];
    actionMap.put(releasedID, new ArmingAction(trigger, command, false));
    inputMap.put(KeyStroke.getKeyStroke("released " + chars[i]), releasedID);
}


comp.add(new JButton("multibindings ... a s d"));

答案 2 :(得分:0)

您可以播放KeyListener的keyPressed和keyReleased方法。如果你用你的键盘输入,keyPressed事件首先在keyReleased之前出现。您可以在按下的键中获取键事件,并捕获与业务逻辑一起发布的键中的组合键。

public void keyPressed(KeyEvent evt){

      keyHit = KeyEvent.getKeyText(evt.getKeyCode());            
      System.out.println("Key Pressed is "+keyHit);

}

public void keyReleased(KeyEvent evt){

      stringBuffer.append(keyHit)      
      System.out.println("Key Released is "+keyHit);

}

答案 3 :(得分:0)

我尝试了很多方法,最后发现了可以被任意两个组合键使用的方法。

下面是我的GUI项目代码:

Boolean k4 = false;
Boolean k5 = false;
Boolean k6 = false;
Boolean ke = false;
Boolean vs = false;
private void jLabel1KeyPressed(java.awt.event.KeyEvent evt) {                                   
int i = evt.getKeyCode();

   switch(i) {
       case  VK_NUMPAD4 :  k4=true;  break;
       case  VK_NUMPAD5 :   k5= true; break;
       case  VK_NUMPAD6 :    k6 = true; break;
       case  VK_ENTER : ke = true; break;
       case   VK_SUBTRACT:  vs=true; break;
   }  
   if(k4==true && ke==true) {

       jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/audit/images/banker.png")));

       k4=false;
       ke=false;

   }
   else if(k5==true &&  ke==true) {

            jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/audit/images/tie.png")));

             k5=false;
             ke=false;
   }
   else if(k6==true && ke==true) {

            jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/audit/images/player.png")));

             k6=false;
             ke=false;
   }

 else if(vs==true && ke==true) {

    jLabel1.requestFocus();
            jLabel1.setIcon(null);

            vs=false;
            ke=false;

   }

  enter_squence();


}