-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonMouse.py
More file actions
154 lines (119 loc) · 5.17 KB
/
PythonMouse.py
File metadata and controls
154 lines (119 loc) · 5.17 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
import pyautogui # Documentation: https://pyautogui.readthedocs.io/en/latest/
from pynput import keyboard
# pyauto GUI references:
# Key names: https://pynput.readthedocs.io/en/latest/keyboard.html
# Cursor movement: https://pyautogui.readthedocs.io/en/latest/mouse.html
# Install: https://stackoverflow.com/questions/67119368/modulenotfounderror-no-module-named-pyautogui
# Give Permissions (Mac): https://stackoverflow.com/questions/62035751/pyautogui-not-running-on-mac-catalina
# Cursor and Keyboard Inputs: https://www.geeksforgeeks.org/mouse-keyboard-automation-using-python/
print("start")
# pyautogui.moveRel(0, 50, duration = 3)
cursorSpeed = 0
maxSpeed = 12
keysPressed = [False, False, False, False] # up down left right
xDirection = 0
yDirection = 0
acceleration = 4
deceleration = 6
mouseToggle = True
# function inspired by: https://stackoverflow.com/questions/11918999/key-listeners-in-python
def pressed_key(key):
global cursorSpeed
global maxSpeed
global keysPressed
global xDirection
global yDirection
global mouseToggle
# catching Attribute error: https://stackoverflow.com/questions/29107534/how-to-catch-nonetype-object-has-no-attribute-something
try:
if (key.name == 'shift_r'): # enter and exit mouse mode
mouseToggle = not mouseToggle
print(mouseToggle)
if (mouseToggle == True):
if (key.name == "right"):
keysPressed[3] = True
xDirection = 1
#print("right")
if (cursorSpeed < maxSpeed):
cursorSpeed = cursorSpeed + acceleration
# print("not max speed yet")
#print(cursorSpeed)
# else:
#print("max speed reached")
#print(cursorSpeed)
if (key.name == "left"):
keysPressed[2] = True
xDirection = -1
if (cursorSpeed < maxSpeed):
cursorSpeed = cursorSpeed + acceleration
#print("not max speed yet")
#print(cursorSpeed)
#else:
#print("max speed reached")
#print(cursorSpeed)
if (key.name == "down"):
keysPressed[1] = True
yDirection = 1 # the origin of the screen is on the top left corner, making it positive to go up
if (cursorSpeed < maxSpeed):
cursorSpeed = cursorSpeed + acceleration
#print("not max speed yet")
#print(cursorSpeed)
#else:
#print("max speed reached")
#print(cursorSpeed)
if (key.name == "up"):
keysPressed[0] = True
yDirection = -1 # the origin of the screen is on the top left corner, making it positive to go up
if (cursorSpeed < maxSpeed):
cursorSpeed = cursorSpeed + acceleration
#print("not max speed yet")
#print(cursorSpeed)
#else:
#print("max speed reached")
#print(cursorSpeed)
if (key.name == "alt_r"):
pyautogui.click()
except:
pass
#print(cursorSpeed*yDirection)
pyautogui.moveRel(cursorSpeed*xDirection, cursorSpeed*yDirection)
#cursorSpeed-=1 # when no arrow keys are being pressed
def release_key(key):
global cursorSpeed
global maxSpeed
global xDirection
global yDirection
global keysPressed
global deceleration
unpressedKeys = 0 # there is no point to decreasing speed if some keys are still pressed
try:
if (key.name == "right"):
keysPressed[3] = False
xDirection = 0
if (key.name == "left"):
keysPressed[2] = False
xDirection = 0
if (key.name == "down"):
keysPressed[1] = False
yDirection = 0
if (key.name == "up"):
keysPressed[0] = False
yDirection = 0
except:
pass
for inputKey in keysPressed:
if (inputKey == False):
unpressedKeys = unpressedKeys + 1
while (unpressedKeys > 0 and cursorSpeed > 0):
cursorSpeed = cursorSpeed - deceleration
#print("decreasing speed")
#print(cursorSpeed)
pyautogui.moveRel(cursorSpeed*xDirection, cursorSpeed*yDirection)
# Key-listeners: https://stackoverflow.com/questions/11918999/key-listeners-in-python
listener = keyboard.Listener(on_press=pressed_key,on_release=release_key)
listener.start() # start to listen on a separate thread
listener.join() # remove if main thread is polling self.keys
program = True
while (program): # keeps program running, have not decided on what key will exit the program.
if (not listener._running):
program = False