-
Notifications
You must be signed in to change notification settings - Fork 0
/
macro.py
70 lines (46 loc) · 1.37 KB
/
macro.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
from pynput.keyboard import Key, Controller, Listener
from pynput.mouse import Controller as MouseController
from pynput.mouse import Button
def sleep_for_frames(sleep_time_ms: int) -> None:
import time
frames = 16.666
sleep_by_frame = (sleep_time_ms * frames) / 1000
time.sleep(sleep_by_frame)
def press_key(key) -> None:
keyboard.press(key)
def release_key(key) -> None:
keyboard.release(key)
def left_click() -> None:
mouse.press(Button.left)
sleep_for_frames(1)
mouse.release(Button.left)
print('Left click pressed')
def on_press(key):
print('{} pressed'.format(key))
try:
valid_char = 'char' in dir(key)
if valid_char and key.char == "u":
left_click()
sleep_for_frames(5)
press_key("r")
press_key(Key.delete)
press_key(Key.shift_r)
sleep_for_frames(1)
release_key("r")
release_key(Key.delete)
release_key(Key.shift_r)
except Exception as ex:
print("Exception:")
print(ex)
def on_release(key):
print('{} release'.format(key))
if key == Key.esc:
# Stop listener
return False
# --- main ---
keyboard = Controller()
mouse = MouseController()
listener = Listener(on_press=on_press, on_release=on_release)
listener.start()
# ... other code ...
listener.join()