Java program to contol computer mouse using the awt Robot and events[转]

转自:http://www.javaprogrammingforums.com/java-se-api-tutorials/214-java-program-contol-computer-mouse-using-awt-robot-events.html

 

Move the mouse cursor position on screen:

 1 import java.awt.Robot;
 2  
 3 public class MouseClass {
 4  
 5  public static void main(String[] args) throws Exception {
 6  
 7             Robot robot = new Robot();
 8  
 9             // SET THE MOUSE X Y POSITION
10             robot.mouseMove(300, 550);
11  
12  }
13 }

 

Click the left mouse button:

 1 import java.awt.Robot;
 2 import java.awt.event.InputEvent;
 3  
 4 public class MouseClass {
 5  
 6  public static void main(String[] args) throws Exception {
 7  
 8             Robot robot = new Robot();
 9  
10             // LEFT CLICK
11             robot.mousePress(InputEvent.BUTTON1_MASK);
12             robot.mouseRelease(InputEvent.BUTTON1_MASK);
13  
14  }
15 }

 

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);
 
 }
}

posted on 2013-10-17 19:50  林盛  阅读(239)  评论(0)    收藏  举报