Skip to content

Commit b85f56f

Browse files
committed
Feature: Add circular mouse movement
1 parent 655d81a commit b85f56f

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
lines changed

src/com/aizuddindeyn/mouse/MouseMove.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@ interface MouseMove {
1515

1616
void move() throws Exception;
1717

18-
default void moveMouse(Point origin, Point target) throws Exception {
19-
Robot robot = MouseRobot.getInstance();
18+
default void move(Point origin, Point target) throws Exception {
2019
double dx = (target.getX() - origin.getX()) / MouseUtils.MOVE_TIMES;
2120
double dy = (target.getY() - origin.getY()) / MouseUtils.MOVE_TIMES;
2221
double dt = 1;
2322
for (int step = 1; step < MouseUtils.MOVE_TIMES; step++) {
2423
Thread.sleep((int) dt);
2524
int x = ((int) (origin.getX() + dx * step));
2625
int y = ((int) (origin.getY() + dy * step));
27-
robot.mouseMove(x, y);
26+
move(new Point(x, y));
2827
}
2928
}
29+
30+
default void move(Point target) throws Exception {
31+
Robot robot = MouseRobot.getInstance();
32+
robot.mouseMove(target.x, target.y);
33+
}
3034
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Owned by aizuddindeyn
3+
* Visit https://gitlab.com/group-bear/mouse-automation
4+
*/
5+
package com.aizuddindeyn.mouse;
6+
7+
import java.awt.Dimension;
8+
import java.awt.Point;
9+
10+
import static com.aizuddindeyn.mouse.MouseUtils.CIRCLE_ROUND_MAX;
11+
12+
/**
13+
* @author aizuddindeyn
14+
* @date 11/7/2020
15+
*/
16+
class MouseMoveCircular implements MouseMove {
17+
18+
@Override
19+
public void move() throws Exception {
20+
Point original = MouseUtils.getMouseLocation();
21+
Dimension screen = MouseUtils.getScreenResolution();
22+
23+
// Calculating best radius based on screen size
24+
int radiusOnHeight = screen.height / 3;
25+
int radiusOnWidth = screen.width / 3;
26+
int radius = Math.min(radiusOnHeight, radiusOnWidth);
27+
28+
int round = MouseRandom.getSecureRandom().nextInt(CIRCLE_ROUND_MAX) + 1;
29+
30+
int midX = (screen.width / 2) + 1;
31+
int midY = (screen.height / 2) + 1;
32+
Point center = new Point(midX, midY);
33+
34+
move(original, center);
35+
36+
Dimension firstPoint = calculateCircle(0, radius);
37+
move(center, new Point(center.x + firstPoint.width, center.y + firstPoint.height));
38+
39+
for (int i = 1; i <= 360 * round; i++) {
40+
Thread.sleep(10);
41+
Dimension d = calculateCircle(i, radius);
42+
move(new Point(center.x + d.width, center.y + d.height));
43+
}
44+
45+
move(MouseUtils.getMouseLocation(), center);
46+
move(center, original);
47+
}
48+
49+
Dimension calculateCircle(int degree, int radius) {
50+
double rad = Math.toRadians(degree);
51+
double x = radius * (Math.cos(rad));
52+
double y = radius * (Math.sin(rad));
53+
54+
return new Dimension((int) x, (int) y);
55+
}
56+
}

src/com/aizuddindeyn/mouse/MouseMoveEdge.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void move() throws Exception {
3838

3939
Point current = p;
4040
for (Point target : targets) {
41-
moveMouse(current, target);
41+
move(current, target);
4242
current = target;
4343
}
4444
}

src/com/aizuddindeyn/mouse/MouseMoveRandom.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ public void move() throws Exception {
3434
y2 = MouseRandom.getSecureRandom().nextInt(screen.height);
3535
} while (x2 == x && y2 == y);
3636

37-
moveMouse(new Point(x, y), new Point(x2, y2));
37+
move(new Point(x, y), new Point(x2, y2));
3838
x = x2;
3939
y = y2;
4040
}
41+
move(MouseUtils.getMouseLocation(), p);
4142
}
4243
}

src/com/aizuddindeyn/mouse/MouseUtils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class MouseUtils {
3030

3131
static final int EDGE_ROUND_MAX = 3;
3232

33+
static final int CIRCLE_ROUND_MAX = 3;
34+
3335
static final long EXECUTOR_DELAY = 100L;
3436

3537
private MouseUtils() {

0 commit comments

Comments
 (0)