-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmaCrow.py
More file actions
203 lines (161 loc) · 6.87 KB
/
Copy pathmaCrow.py
File metadata and controls
203 lines (161 loc) · 6.87 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import time
import json
import argparse
from pynput import mouse as pynput_mouse
import pyautogui
import mouse
import keyboard
import ctypes
import tkinter as tk
from tkinter import Canvas, Label
# Get screen resolution
screen_width, screen_height = pyautogui.size()
# Function to set the cursor position using the Windows API
def set_cursor_pos(x, y):
ctypes.windll.user32.SetCursorPos(x, y)
# Function to display a countdown animation
def countdown_animation(root):
canvas = Canvas(root, width=screen_width, height=screen_height, bg="white", highlightthickness=0)
canvas.pack()
countdown_label = Label(canvas, text="", font=("Helvetica", 50))
countdown_label.place(relx=0.5, rely=0.5, anchor="center")
# for i in range(3, 0, -1):
# countdown_label.config(text=str(i))
# root.update()
# time.sleep(1)
if root.mode == "record":
countdown_label.config(text="Recording has started :3 ")
elif root.mode == "replay":
countdown_label.config(text="Replay has started :3 ")
root.update()
time.sleep(1)
canvas.destroy()
def count_down_animation_config(mode):
print(f"{mode.capitalize()} will start in:")
root = tk.Tk()
root.attributes("-transparentcolor", "white")
root.overrideredirect(1)
root.geometry(f"{screen_width}x{screen_height}+0+0")
root.attributes('-topmost', 1)
root.mode = mode # Set the mode attribute
countdown_animation(root)
root.withdraw() # Hide the root window
def record(filename):
count_down_animation_config("record")
print("Recording started. Move the mouse around, perform actions, and type on the keyboard. Press Ctrl + C to stop.")
actions = []
previous_time = time.time() # Initialize previous_time
# Initialize the mouse listener for scroll and press events
def on_move(x, y):
if 0 <= x < screen_width and 0 <= y < screen_height:
actions.append({
"action": "move",
"position": (x, y),
"time_diff": time_diff
})
def on_click(x, y, button, pressed):
if 0 <= x < screen_width and 0 <= y < screen_height:
actions.append({
"action": "press" if pressed else "release",
"button": str(button),
"position": (x, y),
"time_diff": time_diff
})
def on_scroll(x, y, dx, dy):
if 0 <= x < screen_width and 0 <= y < screen_height:
actions.append({
"action": "scroll",
"position": (x, y),
"scroll": dy,
"time_diff": time_diff
})
def on_key_event(event):
actions.append({
"action": "key",
"key": event.name,
"event_type": event.event_type,
"time_diff": time_diff
})
listener = pynput_mouse.Listener(on_move=on_move, on_click=on_click, on_scroll=on_scroll)
listener.start()
keyboard.hook(on_key_event)
try:
while True:
current_time = time.time()
time_diff = current_time - previous_time
# Get the current mouse position
x, y = mouse.get_position()
# Keep the mouse within the screen boundaries
x = max(0, min(x, screen_width - 1))
y = max(0, min(y, screen_height - 1))
set_cursor_pos(x, y)
time.sleep(0.05) # Adjusted sleep time for smoother recordings
except KeyboardInterrupt:
print("Recording stopped.")
listener.stop()
keyboard.unhook_all()
with open(filename, 'w') as file:
json.dump(actions, file)
# Function to replay mouse and keyboard actions
def replay(filename, key_delay=0.1):
count_down_animation_config("replay")
with open(filename, 'r') as file:
actions = json.load(file)
print("Replaying mouse movements and keyboard inputs...")
# Variables for double click detection
last_click_time = 0
double_click_threshold = 0.3 # Adjust this threshold as needed for your scenario
for i in range(len(actions)):
action = actions[i]
if action["action"] == "move":
# Use mouse.move for smooth movement at normal speed
mouse.move(action["position"][0], action["position"][1], absolute=True, duration=0.00001)
elif action["action"] == "press":
current_time = action["time_diff"]
# Check for double click
if current_time - last_click_time <= double_click_threshold:
if action["button"] == "Button.left":
pyautogui.mouseDown(button='left')
print("double click")
elif action["button"] == "Button.right":
pyautogui.mouseDown(button='right')
print("double right click")
else:
if action["button"] == "Button.left":
pyautogui.mouseDown(button='left')
print("holding mode")
elif action["button"] == "Button.right":
pyautogui.mouseDown(button='right')
print("holding right mode")
last_click_time = current_time
elif action["action"] == "release":
if action["button"] == "Button.left":
pyautogui.mouseUp(button='left')
print("normal click")
elif action["button"] == "Button.right":
pyautogui.mouseUp(button='right')
print("normal right click")
elif action["action"] == "scroll":
# Adjust the sleep time based on the duration of the scroll action
time.sleep(0.01)
mouse.wheel(delta=action["scroll"])
elif action["action"] == "key":
if action["event_type"] == "down":
keyboard.press(action["key"])
print(f"Key pressed: {action['key']}")
elif action["event_type"] == "up":
keyboard.release(action["key"])
print(f"Key released: {action['key']}")
# Introduce a delay between key presses
time.sleep(key_delay)
print("Replay complete.")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Mouse and Keyboard Recorder/Replayer')
parser.add_argument('command', choices=['record', 'replay'], help='Choose command: record or replay')
parser.add_argument('--file', default='mouse_keyboard_actions.json',
help='File to save mouse and keyboard actions (default: mouse_keyboard_actions.json)')
args = parser.parse_args()
if args.command == 'record':
record(args.file)
elif args.command == 'replay':
replay(args.file)