-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyAutoMon.py
More file actions
104 lines (90 loc) · 3.13 KB
/
PyAutoMon.py
File metadata and controls
104 lines (90 loc) · 3.13 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
import os
import pickle
import time
import threading
from pynput import mouse, keyboard
import pyautogui
recordings_folder = 'recordings'
if not os.path.exists(recordings_folder):
os.makedirs(recordings_folder)
actions = []
# Record mouse actions
def on_click(x, y, button, pressed):
timestamp = time.time()
actions.append(('mouse_click', x, y, button, pressed, timestamp))
# Record keyboard actions
def on_press(key):
timestamp = time.time()
actions.append(('keyboard_press', key, timestamp))
def on_release(key):
timestamp = time.time()
actions.append(('keyboard_release', key, timestamp))
# Save actions to a file
def save_actions(filename):
with open(os.path.join(recordings_folder, filename), 'wb') as f:
pickle.dump(actions, f)
# Load actions from a file
def load_actions(filename):
with open(os.path.join(recordings_folder, filename), 'rb') as f:
return pickle.load(f)
# Replay actions
def replay_actions(actions):
start_time = actions[0][-1]
for action in actions:
time.sleep(action[-1] - start_time)
start_time = action[-1]
if action[0] == 'mouse_click':
x, y, button, pressed = action[1], action[2], action[3], action[4]
if pressed:
pyautogui.mouseDown(x=x, y=y, button=button.name)
else:
pyautogui.mouseUp(x=x, y=y, button=button.name)
elif action[0] in ['keyboard_press', 'keyboard_release']:
key = action[1]
if action[0] == 'keyboard_press':
pyautogui.press(str(key).replace("'", ""))
else:
pyautogui.keyUp(str(key).replace("'", ""))
# Record actions
def record():
actions.clear()
mouse_listener = mouse.Listener(on_click=on_click)
keyboard_listener = keyboard.Listener(on_press=on_press, on_release=on_release)
mouse_listener.start()
keyboard_listener.start()
print("Recording... Press ESC to stop.")
def stop_recording(key):
if key == keyboard.Key.esc:
mouse_listener.stop()
keyboard_listener.stop()
return False
with keyboard.Listener(on_press=stop_recording) as listener:
listener.join()
# Menu-driven interface
def menu():
while True:
print("\nMenu:")
print("1. Record actions")
print("2. Save actions")
print("3. Load actions")
print("4. Replay actions")
print("5. Exit")
choice = input("Choose an option: ")
if choice == '1':
record()
elif choice == '2':
filename = input("Enter filename to save: ")
save_actions(filename)
elif choice == '3':
filename = input("Enter filename to load: ")
global actions
actions = load_actions(filename)
print("Actions loaded.")
elif choice == '4':
replay_actions(actions)
elif choice == '5':
break
else:
print("Invalid choice. Please try again.")
if __name__ == "__main__":
menu()