Simulating Mouse and Key Presses

This feature is useful for tools that test windowing applications.
try {
    Robot robot = new Robot();

    // Simulate a mouse click
    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);

    // Simulate a key press
    robot.keyPress(KeyEvent.VK_A);
    robot.keyRelease(KeyEvent.VK_A);
} catch (AWTException e) {
}

Comments

5 Aug 2010 - 1:03pm by Josh Rendon (not verified)

Excellent code snippet. This was really helpful in a program I wrote to automate some administrative tasks.

1 Aug 2011 - 10:24pm by Jayee (not verified)

Just give me how to simulate mouse pointer and clicks with a running example please...

Thanks...

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.