Java 键绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15422488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Keybindings
提问by RADXack
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);
But I need something like
但我需要类似的东西
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);
回答by MadProgrammer
What you're asking is actually counter intuitive and goes against the design of the key bindings API.
您要问的实际上是违反直觉的,并且与键绑定 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.
它可以更轻松地遵循逻辑,进行更改,根据需要规避操作。
But who am I to say what's right :P
但我是谁说的对:P
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.
如果您无法绕过它,一种简单的方法是为每个动作分配一个“命令”,然后您可以在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!");
}
}
}
回答by camickr
You don't have access to the KeyStroke that caused the Action to be executed. So you need to create 4 Actions and pass in a parameter to the Action. Something like:
您无权访问导致执行操作的 KeyStroke。所以需要创建4个Action,并传入一个参数给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());
}
}
You add the Actions to the ActionMap like:
您将操作添加到 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"));
So you share the same basic functionality,but just identify each Action with an identifier.
因此,您共享相同的基本功能,但只需使用标识符标识每个操作。