此应用程序需要在按下 GUI 按钮或箭头键时发送电机移动命令,并在释放 GUI 按钮或箭头键时发送电机停止命令。在按下相应的 GUI 按钮之前,这些绑定适用于向下箭头。按下 GUI 按钮后,箭头键将被忽略。如果是焦点更改杀死了绑定,那么如何将绑定编程为独立于焦点更改?如果不是,真正的问题是什么?如何修复?
键绑定
Action tiltStop = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
MotorStop(TILT_AXIS);
}
};
mainPanel.getInputMap().put(KeyStroke
.getKeyStroke(KeyEvent.VK_DOWN, 0, true), "tiltStop");
mainPanel.getActionMap().put("tiltStop", tiltStop);
Action tiltDown = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
MotorMove(TILT_AXIS, NEGATIVE_DIR);
}
};
mainPanel.getInputMap().put(KeyStroke
.getKeyStroke(KeyEvent.VK_DOWN, 0, false), "tiltDown");
mainPanel.getActionMap().put("tiltDown", tiltDown);
图形用户界面按钮
private void jButtonAxisDownMousePressed(java.awt.event.MouseEvent evt) {
MotorMove(TILT_AXIS, NEGATIVE_DIR);
}
private void jButtonAxisDownMouseReleased(java.awt.event.MouseEvent evt) {
MotorStop(TILT_AXIS);
}