Mamun Sardar
Mamun Sardar

Reputation: 2729

KeyPress and KeyRelease in Robot.java

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.

Upvotes: 2

Views: 4807

Answers (1)

David Kroukamp
David Kroukamp

Reputation: 36423

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?

Upvotes: 2

Related Questions