Last active
October 30, 2019 22:56
-
-
Save corintio/9360688 to your computer and use it in GitHub Desktop.
Port of SRL library's Mouse functions (https://github.com/SRL/SRL-6/blob/master/lib/core/mouse.simba) to Groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Mouse { | |
private Logger log = Logger.getLogger(Mouse) | |
private static MOUSESPEED = 20 | |
private static MouseTracker tracker | |
private IMouse mouse | |
private buttons = [ | |
(PRIMARY): 0x01, | |
(SECONDARY): 0x02, | |
(MIDDLE): 0x04, | |
] | |
static initMouseTracker(IMouse mouse) { | |
if (!tracker) { | |
tracker = new MouseTracker(mouse) | |
} | |
} | |
Mouse(IMouse mouse) { | |
this.mouse = mouse | |
initMouseTracker(mouse) | |
} | |
def click(MouseButton mouseButton) { | |
mouse.putMouseEvent(0, 0, 0, 0, buttons[mouseButton]) | |
long delay = (random() * 90 + 60) | |
sleep(delay) | |
mouse.putMouseEvent(0, 0, 0, 0, 0) | |
} | |
void move(Rectangle region) { | |
// TODO Should randomize the destination coordinates | |
humanWindMouse(tracker.x, tracker.y, region.centerX as Integer, region.centerY as Integer) | |
} | |
private moveMouse(int x, int y) { | |
mouse.putMouseEventAbsolute(x, y, 0, 0, 0) | |
} | |
private humanWindMouse(int xs, int ys, int xe, int ye) { | |
def targetArea = ((random() * MOUSESPEED) / 2.0 + MOUSESPEED); | |
int gravity = 7 | |
int wind = 5 | |
double x = xs | |
double y = ys | |
double veloX = 0, veloY = 0 | |
double veloMag, dist, randomDist, d | |
double windX = 0, windY = 0 | |
int lastX, lastY, w, tDist | |
long timeOut | |
double sqrt2, sqrt3, sqrt5, maxStep, rCnc | |
Point startPoint = new Point(xs, ys) | |
Point endPoint = new Point(xe, ye) | |
sqrt2 = sqrt(2) | |
sqrt3 = sqrt(3) | |
sqrt5 = sqrt(5) | |
tDist = startPoint.distance(endPoint) | |
timeOut = System.currentTimeMillis() + 10000 | |
log.debug("Moving mouse from ($xs, $ys) to ($xe, $ye)") | |
while (System.currentTimeMillis() < timeOut) { | |
dist = hypot(x - xe, y - ye) | |
wind = min(wind, dist) | |
dist = max(dist, 1) | |
d = round((round(tDist) * 0.3) / 7) | |
d = min(d, 25) | |
d = max(d, 5) | |
rCnc = round(random() * 6) | |
if (rCnc == 1) { | |
d = random() + 2 | |
} | |
maxStep = min(d, round(dist)) | |
if (dist >= targetArea) { | |
windX = windX / sqrt3 + (random() * (round(wind) * 2 + 1) - wind) / sqrt5 | |
windY = windY / sqrt3 + (random() * (round(wind) * 2 + 1) - wind) / sqrt5 | |
} else { | |
windX = windX / sqrt2 | |
windY = windY / sqrt2 | |
} | |
veloX += windX | |
veloY += windY | |
veloX += gravity * (xe - x) / dist | |
veloY += gravity * (ye - y) / dist | |
if (hypot(veloX, veloY) > maxStep) { | |
randomDist = maxStep / 2.0 + (random() * (round(maxStep) / 2)) | |
veloMag = sqrt(veloX * veloX + veloY * veloY) | |
veloX = (veloX / veloMag) * randomDist | |
veloY = (veloY / veloMag) * randomDist | |
} | |
lastX = round(x) | |
lastY = round(y) | |
x = x + veloX | |
y = y + veloY | |
if (lastX != round(x) || (lastY != round(y))) { | |
moveMouse(round(x) as int, round(y) as int) | |
} | |
w = random() * (round(100 / MOUSESPEED)) * 6 | |
w = max(w, 5) | |
w = round(w * 0.9) | |
sleep(w) | |
if (hypot(x - xe, y - ye) < 1) { | |
break | |
} | |
} | |
if (round(xe) != round(x) || (round(ye) != round(y))) { | |
moveMouse(xe, ye) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment