Java密钥绑定 [英] Java Keybindings

查看:96
本文介绍了Java密钥绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要绑定所有箭头键来执行相同的功能,但每次都要按下哪个键。目前我只有通过以下方式按下右箭头键时

I need to bind all the arrow keys to perform the same function but each time get which key was pressed. Currently I only have when the right arrow key is pressed via the following

DoneImg.getInputMap(JLabel.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "UpArrow");

Action MvRight = new AbstractAction()
{
     public void actionPerformed(ActionEvent e)
     {
           //Do whatever here
     }
};
DoneImg.getActionMap().put("RightArrow", MvRight);

但我需要类似

DoneImg.getInputMap(JLabel.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "RightArrow");
     DoneImg.getInputMap(JLabel.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "LeftArrow");
     DoneImg.getInputMap(JLabel.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "DownArrow");
     DoneImg.getInputMap(JLabel.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "UpArrow");

     Action MvAll = new AbstractAction()
         {
                 public void actionPerformed(ActionEvent e)
              {
                  if (e.keypressed == "LeftArrow")
                         {System.out.println("The left arrow was pressed!");}
                  if (e.keypressed == "RightArrow")
                         {System.out.println("The right arrow was pressed!");}
                  //and so forth
              }
                 };
     DoneImg.getActionMap().put("RightArrow", MvAll);
     DoneImg.getActionMap().put("LeftArrow", MvAll);
     DoneImg.getActionMap().put("UpArrow", MvAll);
     DoneImg.getActionMap().put("DownArrow", MvAll);


推荐答案

你所问的实际上是反直觉的反对键绑定API的设计。

What you're asking is actually counter intuitive and goes against the design of the key bindings API.

目的是为每个键击提供一个单位的工作。在我看来,这意味着你应该对每个箭头键分别采取行动。

The intention is to provide a single unit of work per key stroke. That would, in my mind, suggest that you should have separate action for each arrow key.

这样可以更容易地遵循逻辑,做出改变,规避行动如你所知。

It makes it much easier to follow the logic, make changes, circumvent the actions as you need.

但我是谁说的是对的:P

But who am I to say what's right :P

如果你看不到的话你绕过它,一种方法很简单,就是为每个动作分配一个命令,然后你可以在 actionPerformed 被触发时查询。

If you can't see you way around it, one way would simple be to assign a "command" to each action that you could then interrogate when the actionPerformed is fired.

public TestKeyBindings02() {
    JPanel panel = new JPanel();
    InputMap im = panel.getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW);
    ActionMap am = panel.getActionMap();

    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, 0), "RightArrow");
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, 0), "LeftArrow");
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_UP, 0), "UpArrow");
    im.put(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, 0), "DownArrow");

    am.put("RightArrow", new ArrowAction("RightArrow"));
    am.put("LeftArrow", new ArrowAction("LeftArrow"));
    am.put("UpArrow", new ArrowAction("UpArrow"));
    am.put("DownArrow", new ArrowAction("DownArrow"));
}

public class ArrowAction extends AbstractAction {

    private String cmd;

    public ArrowAction(String cmd) {
        this.cmd = cmd;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (cmd.equalsIgnoreCase("LeftArrow")) {
            System.out.println("The left arrow was pressed!");
        } else if (cmd.equalsIgnoreCase("RightArrow")) {
            System.out.println("The right arrow was pressed!");
        } else if (cmd.equalsIgnoreCase("UpArrow")) {
            System.out.println("The up arrow was pressed!");
        } else if (cmd.equalsIgnoreCase("DownArrow")) {
            System.out.println("The down arrow was pressed!");
        }
    }
}

这篇关于Java密钥绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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