我想将 ctrl + shift + numpad 7 与动作地图相关联。基本上我正在尝试将我的动作绑定到键盘快捷键,并且想要从顶部按下的数字键或从正确的键盘数字按下的数字键相同的行为。
我能够映射
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK), "Ctrl+4");
但无法映射
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD2, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK), "Ctrl+4");
以下是我要执行的代码:
package testmaik;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class KeyBindingsTest {
public static void main(String[] args) {
new KeyBindingsTest();
}
public KeyBindingsTest() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JLabel state;
/**
*
*/
public TestPane() {
setLayout(new GridBagLayout());
state = new JLabel("Nothing here");
add(state);
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_1, KeyEvent.CTRL_DOWN_MASK), "Ctrl+1");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_2, KeyEvent.CTRL_DOWN_MASK), "Ctrl+2");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD1, KeyEvent.CTRL_DOWN_MASK), "Ctrl+1");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD2, KeyEvent.CTRL_DOWN_MASK), "Ctrl+2");
im.put(KeyStroke.getKeyStroke("ctrl 7"), "ctrl+7");
im.put(KeyStroke.getKeyStroke("ctrl shift 7"), "Ctrl+3");
im.put(KeyStroke.getKeyStroke(KeyEvent.VK_NUMPAD2, KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK), "Ctrl+4");
ActionMap am = getActionMap();
am.put("Ctrl+1", new MessageAction("Ctrl+1"));
am.put("Ctrl+2", new MessageAction("Ctrl+2"));
am.put("Ctrl+3", new MessageAction("Ctrl+3"));
am.put("ctrl+7", new MessageAction("ctrl+7"));
am.put("Ctrl+4", new MessageAction("Ctrl+4"));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
public class MessageAction extends AbstractAction {
private String message;
public MessageAction(String message) {
this.message = message;
}
@Override
public void actionPerformed(ActionEvent e) {
state.setText(message);
}
}
}
}
请帮忙。
答案 0 :(得分:1)
另一种解决方案:尝试使用你的TestPane类,你可以自己捕捉事件:
getRootPane().getContentPane().getToolkit().addAWTEventListener(new AWTEventListener() {
public void eventDispatched(final AWTEvent event) {
if (event.getID() == KeyEvent.KEY_PRESSED) {
final KeyEvent keyEvent = (KeyEvent) event;
switch (keyEvent.getKeyCode()) {
case KeyEvent.VK_NUMPAD7:
if(keyEvent.isControlDown() && keyEvent.isShiftDown()) {
//do Stuff
}
break;
}
}
}
}, AWTEvent.KEY_EVENT_MASK);
我不确定您的活动地图有什么问题。