From 60ea0ac2d479c6b10724c3abc17f69199c2d8214 Mon Sep 17 00:00:00 2001 From: wangbing Date: Mon, 26 Aug 2024 14:14:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=A4=87=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/example/jmacro/wjdr/JMacro.java | 87 ++++++++++++++++++- .../com/example/jmacro/wjdr/JMacroThread.java | 5 ++ .../com/example/jmacro/wjdr/MainTask.java | 14 ++- 3 files changed, 100 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/example/jmacro/wjdr/JMacro.java b/src/main/java/com/example/jmacro/wjdr/JMacro.java index 68805de..8eed18f 100644 --- a/src/main/java/com/example/jmacro/wjdr/JMacro.java +++ b/src/main/java/com/example/jmacro/wjdr/JMacro.java @@ -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. 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); + } + /** * 鼠标滚轮单击 * diff --git a/src/main/java/com/example/jmacro/wjdr/JMacroThread.java b/src/main/java/com/example/jmacro/wjdr/JMacroThread.java index 884664b..1ddf42f 100644 --- a/src/main/java/com/example/jmacro/wjdr/JMacroThread.java +++ b/src/main/java/com/example/jmacro/wjdr/JMacroThread.java @@ -2,6 +2,11 @@ package com.example.jmacro.wjdr; /** * 脚本执行线程 + * + * + * @author wangbing + * @version 0.0.1 + * @since 1.8 */ public abstract class JMacroThread implements Runnable { diff --git a/src/main/java/com/example/jmacro/wjdr/MainTask.java b/src/main/java/com/example/jmacro/wjdr/MainTask.java index d3c99a4..98d1286 100644 --- a/src/main/java/com/example/jmacro/wjdr/MainTask.java +++ b/src/main/java/com/example/jmacro/wjdr/MainTask.java @@ -16,6 +16,13 @@ import java.io.File; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; +/** + * 主线程 + * + * @author wangbing + * @version 0.0.1 + * @since 1.8 + */ public class MainTask { /** @@ -80,19 +87,20 @@ public class MainTask { public MacroThread(JMacro jMacro) { super(jMacro); // 定位mumu bar + Logger.info("定位慕慕窗口"); ScreenRect mumu = locationMuMu(jMacro); if (mumu == null) { throw new IllegalStateException("未检测到MuMu,请开启MuMu模拟器"); } - Logger.info("线程启动成功"); + Logger.info("定位慕慕窗口成功"); // 获取窗口返回 gameScreen = new ScreenRect(); gameScreen.setLeft(mumu.getLeft() - 428); gameScreen.setTop(mumu.getTop() - 8); gameScreen.setRight(mumu.getRight()); gameScreen.setBottom(mumu.getBottom() + 951); - Logger.info("窗口位置:" + gameScreen.toString()); - Logger.info("窗口大小:{}x{}", gameScreen.getRight() - gameScreen.getLeft(), gameScreen.getBottom() - gameScreen.getTop()); + Logger.info("应用窗口位置:" + gameScreen.toString()); + Logger.info("应用窗口大小:{}x{}", gameScreen.getRight() - gameScreen.getLeft(), gameScreen.getBottom() - gameScreen.getTop()); } @Override