java密钥,Java密钥绑定

这篇博客介绍了如何在Java中将多个箭头键(上、下、左、右)绑定到同一事件处理函数,并在函数内部判断是哪个键被按下。通过创建一个自定义Action并传递特定的命令字符串来区分不同键的操作。在接收到ActionEvent时,检查命令字符串以确定触发动作的键。
摘要由CSDN通过智能技术生成

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

解决方案

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.

But who am I to say what's right :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.

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

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值