我希望能够(在JPanel中 - 我使用KeyBindings进行箭头键和ALT + KeyEvent.VK_lots_of_different_keys)按ALT(并且只有ALT)并知道何时按下它以便我可以拖动鼠标周围并阻止它执行按下alt时不应发生的其他事情。
是否有针对此的KeyEvent或解决方法? (我尝试了各种(4)ALT键面罩,但那些显然不起作用)。
一些代码:
altPressed = false;
InputMap inMap = getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actMap = getActionMap();
KeyStroke pressed = KeyStroke.getKeyStroke(KeyEvent.VK_ALT, 0, false);
KeyStroke released = KeyStroke.getKeyStroke(KeyEvent.VK_ALT, 0, true);
inMap.put(pressed, "alt" + "pressed");
inMap.put(released, "alt" + "released");
actMap.put("alt" + "pressed", new AbstractAction () {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
altPressed = true;
}
});
actMap.put("alt" + "released", new AbstractAction () {
private static final long serialVersionUID = 1L;
@Override
public void actionPerformed(ActionEvent e) {
altPressed = false;
}
});
private class KeyListener implements ActionListener {//... yada yada yada ...
@Override
public void actionPerformed(ActionEvent e) {// check if keys are pressed and perform proper actions....
...
...
}
Timer timer = new Timer(100, new KeyListener());
timer.start();
答案 0 :(得分:2)
如果您在单击/拖动/等时尝试检测是否按下了Alt按钮。使用鼠标,您可以使用MouseEvent
MouseEvent
方法。 MouseInputListener
对象将在{{1}}强制执行的每个方法中发送给您的侦听器。