Simulate input/Mouse: Difference between revisions

Content added Content deleted
(→‎{{header|Java}}: {{lines_too_long}} template)
m (→‎{{header|Java}}: Broke a long line)
Line 10: Line 10:
Click 400, 400 right ; relative to top left corner of the screen.</lang>
Click 400, 400 right ; relative to top left corner of the screen.</lang>
=={{header|Java}}==
=={{header|Java}}==
{{lines_too_long}}
You can click on any Component using a Robot and the Component's location:
You can click on any Component using a Robot and the Component's location:
<lang java>Point p = component.getLocation();
<lang java>Point p = component.getLocation();
Robot robot = new Robot();
Robot robot = new Robot();
robot.mouseMove(p.getX(), p.getY()); //you may want to move a few pixels closer to the center by adding to these values
robot.mouseMove(p.getX(), p.getY()); //you may want to move a few pixels closer to the center by adding to these values
robot.mousePress(InputEvent.BUTTON1_MASK); //BUTTON1_MASK is the left button, BUTTON2_MASK is the middle button, BUTTON3_MASK is the right button
robot.mousePress(InputEvent.BUTTON1_MASK); //BUTTON1_MASK is the left button,
//BUTTON2_MASK is the middle button, BUTTON3_MASK is the right button
robot.mouseRelease(InputEvent.BUTTON1_MASK);</lang>
robot.mouseRelease(InputEvent.BUTTON1_MASK);</lang>
If you don't have a reference to the component, you'll need to guess at where it is.
If you don't have a reference to the component, you'll need to guess at where it is.