Java中字符的组合键(非Ctrl或Alt) [英] Key combination of characters (non-Ctrl or Alt) in Java

查看:140
本文介绍了Java中字符的组合键(非Ctrl或Alt)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm doing a project that needs to detect if a certain key combination has been pressed. Any keys like Ctrl, Alt, A-Z and 0-9 can be used as key combination.

我已经使用KeyStroke.getKeyStroke搜索了一些代码,但似乎没有 Ctrl Shift Alt 的情况下不允许组合等

I've searched some codes using KeyStroke.getKeyStroke but it seems like it does not allow combinations without a Ctrl, Shift, Alt, etc.

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

My application needs to detect combinations even WITHOUT Ctrl or Alt e.g. just a combination of plain A+B+C. Is this can also be done withKeyStroke.getKeyStroke? Replies are greatly appreciated.

推荐答案

AFAIK,无法获取键的状态,就像这样:

AFAIK, there is no way to get the status of a key, that is nothing like:

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

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

The only thingy supported is notification about change of a single key's status, f.i. at the lowest level in a KeyListener

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

要处理组合键(特殊的修饰键除外),无法跟踪已按下/释放了哪些键来知道哪些 处于按下状态.

To handle key combinations (except with special modifier keys), there is no way around keeping track of which keys have been pressed/released to know which are in a pressed state.

下面是在keyBindings级别执行此操作的示例.基本成分

Below is an example of doing so on the level of keyBindings. The basic ingredients

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

一些代码:

// 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"));

这篇关于Java中字符的组合键(非Ctrl或Alt)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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