Showing posts with label robot. Show all posts
Showing posts with label robot. Show all posts

Tuesday, March 22, 2011

Control mouse using the Robot Class

For these examples you will need to make sure you import the java.awt.Robot & java.awt.event.InputEvent classes.

Move the mouse cursor position on screen:


import java.awt.Robot;
 
public class MouseClass {
 
 public static void main(String[] args) throws Exception {
 
            Robot robot = new Robot();
 
            // SET THE MOUSE X Y POSITION
            robot.mouseMove(300, 550);
 
 }
}
--------------------------------------------------

Click the left mouse button:


import java.awt.Robot;
import java.awt.event.InputEvent;
 
public class MouseClass {
 
 public static void main(String[] args) throws Exception {
 
            Robot robot = new Robot();
 
            // LEFT CLICK
            robot.mousePress(InputEvent.BUTTON1_MASK);
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
 
 }
}

--------------------------------------------------------------------------------

Click the right mouse button:

import java.awt.Robot;
import java.awt.event.InputEvent;
 
public class MouseClass {
 
 public static void main(String[] args) throws Exception {
 
            Robot robot = new Robot();
 
            // RIGHT CLICK
            robot.mousePress(InputEvent.BUTTON3_MASK);
            robot.mouseRelease(InputEvent.BUTTON3_MASK);
 
 }
}

--------------------------------------------------------------------

Click & scroll the mouse wheel:

import java.awt.Robot;
import java.awt.event.InputEvent;
 
public class MouseClass {
 
 public static void main(String[] args) throws Exception {
 
            Robot robot = new Robot();
 
            // MIDDLE WHEEL CLICK
            robot.mousePress(InputEvent.BUTTON3_DOWN_MASK);
            robot.mouseRelease(InputEvent.BUTTON3_DOWN_MASK);
 
            // SCROLL THE MOUSE WHEEL
            robot.mouseWheel(-100);
 
 }
}