|
| 1 | +""" |
| 2 | +Opens a napari window and records mouse and keyboard interactions to a JSON file |
| 3 | +
|
| 4 | +To begin recording: |
| 5 | +- Run `python record.py` |
| 6 | +
|
| 7 | +To end recording: |
| 8 | +- Hold right click for 2 seconds then release to end the recording for mouse. |
| 9 | +- Press 'ESC' to end the recording for keyboard. |
| 10 | +- Both are needed to finish recording. |
| 11 | +""" |
| 12 | + |
| 13 | +import time |
| 14 | +import json |
| 15 | +import napari |
| 16 | +import argparse |
| 17 | +from pynput import mouse, keyboard |
| 18 | + |
| 19 | + |
| 20 | +class InteractionRecorder: |
| 21 | + def __init__(self, output_file="recording.json"): |
| 22 | + self.recording = [] |
| 23 | + self.output_file = output_file |
| 24 | + self.keyboard_listener = None |
| 25 | + self.mouse_listener = None |
| 26 | + self.viewer = None |
| 27 | + |
| 28 | + def on_press(self, key): |
| 29 | + try: |
| 30 | + json_object = {"action": "pressed_key", "key": key.char, "_time": time.time()} |
| 31 | + except AttributeError: |
| 32 | + if key == keyboard.Key.esc: |
| 33 | + print("Keyboard recording ended.") |
| 34 | + self.stop_recording() |
| 35 | + return False |
| 36 | + |
| 37 | + json_object = {"action": "pressed_key", "key": str(key), "_time": time.time()} |
| 38 | + |
| 39 | + self.recording.append(json_object) |
| 40 | + |
| 41 | + def on_release(self, key): |
| 42 | + try: |
| 43 | + json_object = {"action": "released_key", "key": key.char, "_time": time.time()} |
| 44 | + except AttributeError: |
| 45 | + json_object = {"action": "released_key", "key": str(key), "_time": time.time()} |
| 46 | + |
| 47 | + self.recording.append(json_object) |
| 48 | + |
| 49 | + def on_move(self, x, y): |
| 50 | + if len(self.recording) >= 1: |
| 51 | + if ( |
| 52 | + self.recording[-1]["action"] == "pressed" |
| 53 | + and self.recording[-1]["button"] == "Button.left" |
| 54 | + ) or ( |
| 55 | + self.recording[-1]["action"] == "moved" |
| 56 | + and time.time() - self.recording[-1]["_time"] > 0.02 |
| 57 | + ): |
| 58 | + json_object = {"action": "moved", "x": x, "y": y, "_time": time.time()} |
| 59 | + self.recording.append(json_object) |
| 60 | + |
| 61 | + def on_click(self, x, y, button, pressed): |
| 62 | + json_object = { |
| 63 | + "action": "clicked" if pressed else "unclicked", |
| 64 | + "button": str(button), |
| 65 | + "x": x, |
| 66 | + "y": y, |
| 67 | + "_time": time.time(), |
| 68 | + } |
| 69 | + |
| 70 | + self.recording.append(json_object) |
| 71 | + |
| 72 | + if len(self.recording) > 1: |
| 73 | + if ( |
| 74 | + self.recording[-1]["action"] == "unclicked" |
| 75 | + and self.recording[-1]["button"] == "Button.right" |
| 76 | + and self.recording[-1]["_time"] - self.recording[-2]["_time"] > 2 |
| 77 | + ): |
| 78 | + self.save_recording() |
| 79 | + print("Mouse recording ended.") |
| 80 | + self.stop_recording() |
| 81 | + return False |
| 82 | + |
| 83 | + def on_scroll(self, x, y, dx, dy): |
| 84 | + json_object = { |
| 85 | + "action": "scroll", |
| 86 | + "vertical_direction": int(dy), |
| 87 | + "horizontal_direction": int(dx), |
| 88 | + "x": x, |
| 89 | + "y": y, |
| 90 | + "_time": time.time(), |
| 91 | + } |
| 92 | + |
| 93 | + self.recording.append(json_object) |
| 94 | + |
| 95 | + def save_recording(self): |
| 96 | + """Save the recorded interactions to a JSON file.""" |
| 97 | + with open(self.output_file, "w") as f: |
| 98 | + json.dump(self.recording, f) |
| 99 | + print(f"Recording saved to {self.output_file}") |
| 100 | + |
| 101 | + def start_recording(self): |
| 102 | + """Creates a napari window and starts recording mouse and keyboard interactions.""" |
| 103 | + napari.Viewer.close_all() |
| 104 | + # Open napari with standard window size (800x600) |
| 105 | + self.viewer = napari.Viewer() |
| 106 | + self.viewer.window._qt_window.setGeometry(0, 0, 800, 600) |
| 107 | + |
| 108 | + # Reset recording for this session |
| 109 | + self.recording = [] |
| 110 | + |
| 111 | + print("Press 'ESC' to end the keyboard recording") |
| 112 | + print("Hold right click for 2 seconds then release to end the mouse recording") |
| 113 | + |
| 114 | + self.keyboard_listener = keyboard.Listener( |
| 115 | + on_press=self.on_press, |
| 116 | + on_release=self.on_release |
| 117 | + ) |
| 118 | + self.mouse_listener = mouse.Listener( |
| 119 | + on_click=self.on_click, |
| 120 | + on_scroll=self.on_scroll, |
| 121 | + on_move=self.on_move |
| 122 | + ) |
| 123 | + |
| 124 | + self.keyboard_listener.start() |
| 125 | + self.mouse_listener.start() |
| 126 | + |
| 127 | + # Show the napari window |
| 128 | + napari.run() |
| 129 | + |
| 130 | + # Wait for listeners to finish |
| 131 | + self.keyboard_listener.join() |
| 132 | + self.mouse_listener.join() |
| 133 | + |
| 134 | + def stop_recording(self): |
| 135 | + """Stop the recording and save the data.""" |
| 136 | + if self.keyboard_listener: |
| 137 | + self.keyboard_listener.stop() |
| 138 | + if self.mouse_listener: |
| 139 | + self.mouse_listener.stop() |
| 140 | + self.save_recording() |
| 141 | + |
| 142 | + if self.viewer: |
| 143 | + self.viewer.close() |
| 144 | + |
| 145 | + |
| 146 | +def start_recording(output: str = "recording.json"): |
| 147 | + """Creates a napari window and starts recording mouse and keyboard interactions. |
| 148 | +
|
| 149 | + Args: |
| 150 | + output (str): The name of the output JSON file to save the recording. |
| 151 | + """ |
| 152 | + recorder = InteractionRecorder(output) |
| 153 | + recorder.start_recording() |
| 154 | + |
| 155 | + |
| 156 | +if __name__ == "__main__": |
| 157 | + parser = argparse.ArgumentParser( |
| 158 | + description="Record mouse and keyboard interactions in a napari window." |
| 159 | + ) |
| 160 | + parser.add_argument( |
| 161 | + "output", |
| 162 | + type=str, |
| 163 | + nargs='?', |
| 164 | + default="recording.json", |
| 165 | + help="Output JSON file name" |
| 166 | + ) |
| 167 | + args = parser.parse_args() |
| 168 | + start_recording(args.output) |
0 commit comments