Question

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.

Was it helpful?

Solution

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top