Java密钥绑定

时间:2013-03-15 00:04:58

标签: java key-bindings

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

按下右箭头键
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);

2 个答案:

答案 0 :(得分:10)

你所问的实际上是反直觉的,并且违背了密钥绑定API的设计。

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

它可以更轻松地遵循逻辑,进行更改,根据需要规避行动。

但我该说谁是对的:P

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

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!");
        }
    }
}

答案 1 :(得分:2)

您无权访问导致Action执行的KeyStroke。因此,您需要创建4个动作并将参数传递给Action。类似的东西:

class SomeAction extends AbstractAction
{
    public SomeAction(String name)
    {
        putValue(Action.NAME, name);
        putValue(ACTION_COMMAND_KEY, "Command: " + name);
    }

    public void actionPerformed(ActionEvent e)
    {
        System.out.println("Name: " + getValue(Action.NAME) );
        System.out.println(e.getActionCommand());
    }
}

您可以将动作添加到ActionMap,如:

DoneImg.getActionMap().put("RightArrow", new SomeAction("RightArrow"));
DoneImg.getActionMap().put("LeftArrow", new SomeAction("LeftArrow"));
DoneImg.getActionMap().put("UpArrow", new SomeAction("UpArrow"));
DoneImg.getActionMap().put("DownArrow", new SomeAction("DownArrow"));

因此,您共享相同的基本功能,但只需使用标识符识别每个Action。