-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSlowMover.py
81 lines (66 loc) · 2.4 KB
/
SlowMover.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import threading
from talon import cron, actions, Module, ctrl, settings
from math import copysign
mod = Module()
mod.setting(
"screen_spots_slow_move_distance",
type=int,
default=200,
desc="the maximum distance to move in either direction per tick during slow movement",
)
class SlowMover:
def __init__(self):
self.job = None
self.lock = threading.RLock()
self.targets = []
def slowly_move_to(self, x, y):
with self.lock:
self.targets.append((x, y))
if self.job is None:
self.start()
def slowly_click(self):
with self.lock:
self.targets.append('click')
if self.job is None:
self.start()
def start(self):
with self.lock:
cron.cancel(self.job)
self.job = cron.interval('16ms', self.tick)
def stop(self):
with self.lock:
cron.cancel(self.job)
self.job = None
def tick(self):
with self.lock:
if not self.targets:
self.stop()
next_target = self.targets[0]
if type(next_target) is tuple:
x, y = next_target
self.small_movement(x, y)
else:
ctrl.mouse_click(button=0, hold=16000)
self.targets.pop(0)
def small_movement(self, target_x, target_y):
current_x = actions.mouse_x()
current_y = actions.mouse_y()
x_distance = target_x - current_x
y_distance = target_y - current_y
if abs(x_distance) > settings.get('user.screen_spots_slow_move_distance'):
x_distance = copysign(settings.get('user.screen_spots_slow_move_distance'), x_distance)
if abs(y_distance) > settings.get('user.screen_spots_slow_move_distance'):
y_distance = copysign(settings.get('user.screen_spots_slow_move_distance'), y_distance)
if x_distance == 0 and y_distance == 0:
self.targets.pop(0)
else:
actions.mouse_move(current_x + x_distance, current_y + y_distance)
mover = SlowMover()
@mod.action_class
class SlowMoveActions:
def slow_mouse_move(x: int, y: int):
"""Move the cursor to a new position non instantly"""
mover.slowly_move_to(x, y)
def slow_mouse_click():
"""Click the mouse once the cursor has reached the position it is moving to"""
mover.slowly_click()