I am trying to simulate a keypress and hold (in java) while the mouse is on top of a button. The keypress should only stop when the mouse is no longer over the button. I have the keypress working but not to keep it pressed. What is the best way to do this? I tried never-ending loops but then it does not stop when the mouse exits (obviously).
Here is my somewhat working code:
buttonSD = new JButton("S+D");
buttonSD.addMouseListener(new MouseAdapter(){
public void mouseEntered(MouseEvent e){
CoordsLabel.setText("Bottom Right");
currentBtn.keyPress(KeyEvent.VK_S);
currentBtn2.keyPress(KeyEvent.VK_D);
}
public void mouseExited(MouseEvent e){
currentBtn.keyRelease(KeyEvent.VK_S);
currentBtn2.keyRelease(KeyEvent.VK_S);
}
});
c.weightx = .25;
c.weighty = .25;
c.gridx = 2;
c.gridy = 2;
gridbag.setConstraints(buttonSD, c);
controlFrame.add(buttonSD);
try{
currentBtn = new Robot();
currentBtn2 = new Robot();
} catch (AWTException e){
e.printStackTrace();
}
Thanks in advance!