import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
public class App {
public static void main(String[] args) {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
//mouserClick(100,15);
Point point = MouseInfo.getPointerInfo().getLocation();
System.out.println("x=" + point.getX() + ",y="+ point.getY());
//mouserClick(2246,20);
}
private static void mouserClick(int x, int y) {
//初始化robot
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
//robot.delay(300);
//鼠标移动到某一点
robot.mouseMove(x, y);
//模拟鼠标按下左键
robot.mousePress(InputEvent.BUTTON1_MASK);
//模拟鼠标松开左键
robot.mouseRelease(InputEvent.BUTTON1_MASK);
/*robot.keyPress(KeyEvent.VK_ALT); // 按下Ctrl键
robot.keyPress(KeyEvent.VK_TAB); // 按下R键
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
robot.keyRelease(KeyEvent.VK_TAB); // 释放R键
robot.keyRelease(KeyEvent.VK_ALT); // 释放Ctrl键
*/
//InputEvent.BUTTON2_MASK表示鼠标中键
//InputEvent.BUTTON3_MASK表示鼠标右键
//robot还可以模拟键盘点击,如有需要请自行百度
}
}
java使用robot实现控制电脑
最新推荐文章于 2024-08-23 09:07:57 发布