Last active
May 3, 2024 21:14
-
-
Save s4lt3d/12c07cfec79bdcb4c31ae500b81fe776 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from pynput import mouse, keyboard | |
| import threading | |
| import time | |
| class Recorder: | |
| def __init__(self): | |
| self.recording = False | |
| self.actions = [] | |
| self.start_time = None | |
| self.playback_thread = None | |
| self.stop_loop = threading.Event() | |
| def start_recording(self): | |
| self.recording = True | |
| self.actions = [] | |
| self.start_time = time.time() | |
| print("Recording started.") | |
| def stop_recording(self): | |
| self.recording = False | |
| print("Recording stopped.") | |
| def record_click(self, x, y, button, pressed): | |
| if self.recording: | |
| action_time = time.time() - self.start_time | |
| self.actions.append(('click', action_time, x, y, button, pressed)) | |
| def record_move(self, x, y): | |
| if self.recording: | |
| action_time = time.time() - self.start_time | |
| self.actions.append(('move', action_time, x, y)) | |
| def record_keystroke(self, key, modifiers): | |
| if self.recording: | |
| action_time = time.time() - self.start_time | |
| self.actions.append(('keystroke', action_time, key, modifiers)) | |
| def playback(self, loop=False): | |
| self.stop_loop.clear() | |
| while True: | |
| print("Playback starting...") | |
| start_time = time.time() | |
| for action in self.actions: | |
| if self.stop_loop.is_set(): | |
| print("Playback interrupted.") | |
| return | |
| while time.time() - start_time < action[1]: | |
| time.sleep(0.01) # Use sleep to yield control and reduce CPU usage | |
| if action[0] == 'move': | |
| mouse_controller.position = (action[2], action[3]) | |
| elif action[0] == 'click': | |
| if action[5]: # Pressed | |
| mouse_controller.press(action[4]) | |
| else: | |
| mouse_controller.release(action[4]) | |
| elif action[0] == 'keystroke': | |
| for modifier in action[3]: | |
| keyboard_controller.press(modifier) | |
| keyboard_controller.tap(action[2]) | |
| for modifier in action[3]: | |
| keyboard_controller.release(modifier) | |
| if not loop: | |
| break | |
| print("Playback finished.") | |
| def start_playback(self, loop=False): | |
| if self.playback_thread is None or not self.playback_thread.is_alive(): | |
| self.playback_thread = threading.Thread(target=self.playback, args=(loop,)) | |
| self.playback_thread.start() | |
| else: | |
| print("Playback already in progress.") | |
| def stop_playback(self): | |
| self.stop_loop.set() | |
| if self.playback_thread: | |
| self.playback_thread.join() | |
| recorder = Recorder() | |
| mouse_controller = mouse.Controller() | |
| keyboard_controller = keyboard.Controller() | |
| current_keys = set() | |
| modifiers = {keyboard.Key.shift, keyboard.Key.shift_l, keyboard.Key.shift_r, | |
| keyboard.Key.ctrl, keyboard.Key.ctrl_l, keyboard.Key.ctrl_r, | |
| keyboard.Key.alt, keyboard.Key.alt_l, keyboard.Key.alt_r, | |
| keyboard.Key.cmd, keyboard.Key.cmd_l, keyboard.Key.cmd_r} | |
| def on_click(x, y, button, pressed): | |
| recorder.record_click(x, y, button, pressed) | |
| def on_move(x, y): | |
| recorder.record_move(x, y) | |
| def on_press(key): | |
| if hasattr(key, 'char'): | |
| current_keys.add(key.char) | |
| else: | |
| current_keys.add(key) | |
| active_modifiers = [k for k in current_keys if k in modifiers] | |
| if key not in modifiers and key not in [keyboard.Key.f1, keyboard.Key.f2, keyboard.Key.f3, keyboard.Key.f4, keyboard.Key.f5, keyboard.Key.f6]: | |
| recorder.record_keystroke(key, active_modifiers) | |
| if {keyboard.Key.shift, keyboard.Key.f2} <= current_keys: | |
| recorder.start_recording() | |
| elif {keyboard.Key.shift, keyboard.Key.f3} <= current_keys: | |
| recorder.stop_recording() | |
| elif {keyboard.Key.shift, keyboard.Key.f4} <= current_keys: | |
| recorder.start_playback(loop=False) | |
| elif {keyboard.Key.shift, keyboard.Key.f6} <= current_keys: | |
| if recorder.playback_thread and recorder.playback_thread.is_alive(): | |
| recorder.stop_playback() | |
| else: | |
| recorder.start_playback(loop=True) | |
| def on_release(key): | |
| if hasattr(key, 'char'): | |
| current_keys.discard(key.char) | |
| else: | |
| current_keys.discard(key) | |
| with mouse.Listener(on_click=on_click, on_move=on_move) as mouse_listener, keyboard.Listener(on_press=on_press, on_release=on_release) as key_listener: | |
| key_listener.join() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Records most mouse and keyboard strokes with time.
Shift + F2 to record
Shift + F3 to stop
Shift + F4 to playback once
Shift + F6 to playback on loop
Shift + F6 again to stop loop playback