-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmouse.py
149 lines (104 loc) · 3.88 KB
/
mouse.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# from https://github.com/talonvoice/examples
# jsc added shift-click, command-click, and voice code compatibility
# import eye
import time
from talon import ctrl, tap, ui
from talon.voice import Context, Key
ctx = Context("mouse")
x, y = ctrl.mouse_pos()
mouse_history = [(x, y, time.time())]
force_move = None
def on_move(typ, e):
mouse_history.append((e.x, e.y, time.time()))
if force_move:
e.x, e.y = force_move
return True
tap.register(tap.MMOVE, on_move)
def click_pos(m):
word = m._words[0]
start = (word.start + min((word.end - word.start) / 2, 0.100)) / 1000.0
diff, pos = min([(abs(start - pos[2]), pos) for pos in mouse_history])
return pos[:2]
def delayed_click(m, button=0, times=1):
# old = eye.config.control_mouse
# eye.config.control_mouse = False
# x, y = click_pos(m)
# ctrl.mouse(x, y)
ctrl.mouse_click(x, y, button=button, times=times, wait=16000)
# time.sleep(0.032)
# eye.config.control_mouse = old
def press_key_and_click(m, key, button=0, times=1):
ctrl.key_press(key, down=True)
ctrl.mouse_click(x, y, button=button, times=times, wait=16000)
ctrl.key_press(key, up=True)
def shift_click(m, button=0, times=1):
press_key_and_click(m, "shift", button, times)
def command_click(m, button=0, times=1):
press_key_and_click(m, "cmd", button, times)
def delayed_right_click(m):
delayed_click(m, button=1)
def delayed_dubclick(m):
delayed_click(m, button=0, times=2)
def delayed_tripclick(m):
delayed_click(m, button=0, times=3)
def mouse_scroll(amount):
def scroll(m):
ctrl.mouse_scroll(y=amount)
return scroll
def mouse_smooth_scroll(amount, SCROLL_TOTAL_TIME=0):
def scroll(m):
if SCROLL_TOTAL_TIME != 0:
interval = 0.007
depth = int(SCROLL_TOTAL_TIME // interval)
split = amount / depth
for x in range(depth):
ctrl.mouse_scroll(y=split)
time.sleep(interval)
else:
ctrl.mouse_scroll(y=amount)
return scroll
def mouse_drag(m):
x, y = click_pos(m)
ctrl.mouse_click(x, y, down=True)
def mouse_release(m):
x, y = click_pos(m)
ctrl.mouse_click(x, y, up=True)
def mouse_center(m):
win = ui.active_window()
rect = win.rect
center = (rect.x + rect.width / 2, rect.y + rect.height / 2)
print(rect, center)
ctrl.mouse_move(*center)
def press_key_and_click(m, key, button=0, times=1):
ctrl.key_press(key, down=True)
ctrl.mouse_click(x, y, button=button, times=times, wait=16000)
ctrl.key_press(key, up=True)
def shift_click(m, button=0, times=1):
press_key_and_click(m, "shift", button, times)
def command_click(m, button=0, times=1):
press_key_and_click(m, "cmd", button, times)
def control_shift_click(m, button=0, times=1):
ctrl.key_press("shift", ctrl=True, shift=True, down=True)
ctrl.mouse_click(x, y, button=button, times=times, wait=16000)
ctrl.key_press("shift", ctrl=True, shift=True, up=True)
keymap = {
# jsc modified with some voice-code compatibility
"righty": delayed_right_click,
"(click)": delayed_click,
"(dubclick | duke)": delayed_dubclick,
"(tripclick | triplick)": delayed_tripclick,
"drag": mouse_drag,
"drag release": mouse_release,
"(shift click)": shift_click,
"(command click | chom lick)": command_click,
# "(control shift click | troll shift click)" : control_shift_click,
# "(control shift double click | troll shift double click)" : lambda m: control_shift_click(m, 0, 2),
"do park": [delayed_dubclick, Key('cmd-v')],
"do koosh": [delayed_dubclick, Key('cmd-c')],
"wheel down": mouse_smooth_scroll(350, 0.11),
"wheel up": mouse_smooth_scroll(-350, 0.11),
"wheel down here": [mouse_center, mouse_scroll(200)],
"wheel up here": [mouse_center, mouse_scroll(-200)],
"mouse center": mouse_center,
}
ctx.keymap(keymap)