|
|
|
@ -9,18 +9,24 @@ import com.example.jmacro.wjdr.util.TaskUtil;
|
|
|
|
|
|
|
|
|
|
import java.awt.*;
|
|
|
|
|
import java.awt.event.InputEvent;
|
|
|
|
|
import java.awt.event.KeyEvent;
|
|
|
|
|
import java.awt.image.BufferedImage;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Java脚本精灵
|
|
|
|
|
*
|
|
|
|
|
* @author wangbing
|
|
|
|
|
* @version 0.0.1
|
|
|
|
|
* @since 1.8
|
|
|
|
|
*/
|
|
|
|
|
public class JMacro {
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) throws AWTException {
|
|
|
|
|
JMacro jMacro = new JMacro();
|
|
|
|
|
jMacro.mouseMove(new ScreenPoint(100, 100));
|
|
|
|
|
jMacro.mouseLeftClick(new ScreenPoint(115, 614));
|
|
|
|
|
jMacro.keyInput(KeyEvent.VK_A, KeyEvent.VK_B, KeyEvent.VK_C, KeyEvent.VK_D, KeyEvent.VK_ENTER);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -29,7 +35,26 @@ public class JMacro {
|
|
|
|
|
private final Robot robot;
|
|
|
|
|
|
|
|
|
|
public JMacro() throws AWTException {
|
|
|
|
|
// 机器人初始化
|
|
|
|
|
this.robot = new Robot();
|
|
|
|
|
this.robot.setAutoDelay(100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 键盘按键组
|
|
|
|
|
*/
|
|
|
|
|
public void keyInput(int... keycodes) {
|
|
|
|
|
for (int keycode : keycodes) {
|
|
|
|
|
keyPress(keycode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 键盘按键
|
|
|
|
|
* keycode Key to press (e.g. <code>KeyEvent.VK_A)
|
|
|
|
|
*/
|
|
|
|
|
public void keyPress(int keycode) {
|
|
|
|
|
this.robot.keyPress(keycode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -51,7 +76,6 @@ public class JMacro {
|
|
|
|
|
if (smooth) {
|
|
|
|
|
// 获取当前鼠标位置
|
|
|
|
|
Point mousePoint = MouseInfo.getPointerInfo().getLocation();
|
|
|
|
|
|
|
|
|
|
int startX = mousePoint.x;
|
|
|
|
|
int startY = mousePoint.y;
|
|
|
|
|
|
|
|
|
@ -60,13 +84,16 @@ public class JMacro {
|
|
|
|
|
double absY = Math.abs(startY - point.getY());
|
|
|
|
|
double absZ = Math.sqrt(Math.pow(absX, 2) + Math.pow(absY, 2));
|
|
|
|
|
int times = (int) (absZ / 30 + (absZ % 30 > 0 ? 1 : 0));
|
|
|
|
|
int interval = Math.min(500 / times, 10);
|
|
|
|
|
|
|
|
|
|
times = Math.min(times, 10);
|
|
|
|
|
// 分times次移动到指定点
|
|
|
|
|
for (int i = 1; i <= times; i++) {
|
|
|
|
|
float d = i * 1.0f / times;
|
|
|
|
|
int dx = (int) (startX + (point.getX() - startX) * d);
|
|
|
|
|
int dy = (int) (startY + (point.getY() - startY) * d);
|
|
|
|
|
robot.mouseMove(dx, dy);
|
|
|
|
|
robot.delay(RandomUtil.randomInt(5, 20));
|
|
|
|
|
robot.delay(RandomUtil.randomInt(interval - 10, interval + 10));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
robot.mouseMove(point.getX(), point.getY());
|
|
|
|
@ -94,6 +121,60 @@ public class JMacro {
|
|
|
|
|
mouseLeftClick(new ScreenPoint(rect.getCenter()[0], rect.getCenter()[1]));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 鼠标左键拖拽
|
|
|
|
|
*
|
|
|
|
|
* @param start 开始位置
|
|
|
|
|
* @param end 结束位置
|
|
|
|
|
*/
|
|
|
|
|
public void mouseLeftDrag(ScreenPoint start, ScreenPoint end) {
|
|
|
|
|
mouseLeftDrag(start, end, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 鼠标左键拖拽
|
|
|
|
|
*
|
|
|
|
|
* @param start 开始位置
|
|
|
|
|
* @param end 结束位置
|
|
|
|
|
* @param smooth 平滑移动
|
|
|
|
|
*/
|
|
|
|
|
public void mouseLeftDrag(ScreenPoint start, ScreenPoint end, boolean smooth) {
|
|
|
|
|
mouseMove(start, smooth);
|
|
|
|
|
waitTap();
|
|
|
|
|
robot.mousePress(InputEvent.BUTTON1_MASK);
|
|
|
|
|
waitTap();
|
|
|
|
|
mouseMove(end, smooth);
|
|
|
|
|
waitTap();
|
|
|
|
|
robot.mouseRelease(InputEvent.BUTTON1_MASK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 鼠标右键拖拽
|
|
|
|
|
*
|
|
|
|
|
* @param start 开始位置
|
|
|
|
|
* @param end 结束位置
|
|
|
|
|
*/
|
|
|
|
|
public void mouseRightDrag(ScreenPoint start, ScreenPoint end) {
|
|
|
|
|
mouseRightDrag(start, end, false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 鼠标右键拖拽
|
|
|
|
|
*
|
|
|
|
|
* @param start 开始位置
|
|
|
|
|
* @param end 结束位置
|
|
|
|
|
* @param smooth 平滑移动
|
|
|
|
|
*/
|
|
|
|
|
public void mouseRightDrag(ScreenPoint start, ScreenPoint end, boolean smooth) {
|
|
|
|
|
mouseMove(start, smooth);
|
|
|
|
|
waitTap();
|
|
|
|
|
robot.mousePress(InputEvent.BUTTON3_MASK);
|
|
|
|
|
waitTap();
|
|
|
|
|
mouseMove(end, smooth);
|
|
|
|
|
waitTap();
|
|
|
|
|
robot.mouseRelease(InputEvent.BUTTON3_MASK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 鼠标滚轮单击
|
|
|
|
|
*
|
|
|
|
|