-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse_control.py
More file actions
171 lines (135 loc) · 4.42 KB
/
Copy pathmouse_control.py
File metadata and controls
171 lines (135 loc) · 4.42 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# -*- coding: utf-8 -*-
"""
Class to simplify the use of the mouse.
"""
from pynput import mouse
from screeninfo import get_monitors
class MouseControl(object):
"""Class to simplify the use of the mouse"""
def __init__(self):
super(MouseControl, self).__init__()
self.mouse_buttons = {
'left': mouse.Button.left,
'middle': mouse.Button.middle,
'right': mouse.Button.right
}
self.transpose_button = {key: value for (value, key) in self.mouse_buttons.items()}
self.on_move_f = None
self.on_click_f = None
self.on_scroll_f = None
self.init()
def init(self):
"""
Runs some init functions.
- Inits controller.
"""
self.controller = mouse.Controller()
def mouse_move(self, move=None, rel=False, drag=False, function=None, iters=1):
""""
Moves the mouse taking care of:
- If receives a function, executes it before each cycle.
- If rel, enables relative move.
- If drag, moves the mouse with the left button pressed.
- If move == None, return the current position of the mouse.
"""
if move == None:
return self.controller.position()
if drag:
self.controller.press(mouse.Button.left)
if rel:
self.controller.move(*move)
else:
self.controller.position = move
self.controller.release(self.mouse_buttons['left'])
if function:
for i in range(iters):
x, y = function()
self.controller.position = (x, y)
else:
if rel:
self.controller.move(*move)
else:
self.controller.position = move
def click(self, button, clicks=1):
"""
Click in the current position of the mouse.
"""
button = self.mouse_buttons[button]
self.controller.click(button, clicks)
def scroll(self, amount, percent=False):
"""
Scrolls up or down.
- If percent, moves that percent of the screen.
"""
if percent:
first_monitor_height = get_monitors()[0].height
amount = (amount // 100) * first_monitor_height
self.controller.scroll(0, -amount)
# --------------------------------------------------
# --------------- MOUSE MONITORING -----------------
# --------------------------------------------------
def on_move(self, x, y):
"""
On move event.
"""
if self.on_move_f != None:
self.on_move_f(x, y)
def on_click(self, x, y, button, pressed):
"""
On click event.
"""
button = self.transpose_button[button]
if self.on_click_f != None:
self.on_click_f(x, y, button, pressed)
def on_scroll(self, x, y, dx, dy):
"""
On scroll event.
"""
if self.on_scroll_f != None:
self.on_scroll_f(dx, dy)
def start_listening(
self,
on_move=None,
on_click=None,
on_scroll=None,
blocking=False
):
"""
Function that configures the listening using
arguments to set all properties.
"""
self.on_move_f = on_move
self.on_click_f = on_click
self.on_scroll_f = on_scroll
if blocking:
with mouse.Listener(
on_move=self.on_move,
on_click=self.on_click,
on_scroll=self.on_scroll
) as listener:
listener.join()
else:
listener = mouse.Listener(
on_move=self.on_move,
on_click=self.on_click,
on_scroll=self.on_scroll
)
listener.start()
# --------------------------------------------------
# --------------- MAIN EXAMPLE -----------------
# --------------------------------------------------
def on_move_p(x, y):
print(x, ':', y)
def on_click_p(x, y, button, pressed):
print(x, ':', y, ':', button, ':', pressed)
def on_scroll_p(dx, dy):
print(dx, ':', dy)
def main():
mouse_controller = MouseControl()
mouse_controller.start_listening(
on_move=on_move_p,
on_click=on_click_p,
on_scroll=on_scroll_p,
blocking=True)
if __name__ == "__main__":
main()