2

In a button action I tried to add a keypress.

try{
    Robot robot=new Robot();

    robot.keyPress(KeyEvent.VK_A);
    robot.keyRelease(KeyEvent.VK_A);

    robot.mousePress(KeyEvent.BUTTON1_MASK);
    robot.mouseRelease(KeyEvent.BUTTON1_MASK);
}catch(Exception e){}

Now I want the "A-Key" hold,In that case I commented out the third line
robot.keyRelease(KeyEvent.VK_A);
But the keypress doesn't need the keyRelease.So I didn't get the "A-Key" hold. In another case mousePress is pressed until the mouseRelease does not execute.Can anybody give me solution or explain what's happening.

Mamun Sardar
  • 2,679
  • 4
  • 36
  • 44

1 Answers1

2

See a similar question, whose answer uses Thread.sleep() to keep the key pressed down: Simulate a key held down in Java and this question: How can I make Robot press and hold a mouse button for a certain period of time?

Community
  • 1
  • 1
David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
  • 1
    I wonder it's not the similar question.My mousePress and mouseRelease worked. but the Keypress doesn't need the keyRelease. Even If I don't add the keyRelease it automatically release that key. – Mamun Sardar Jul 27 '12 at 14:27