From babcdd262abc4c208c27cbe036ee33e9a420543b Mon Sep 17 00:00:00 2001 From: Adam Saudagar Date: Sat, 17 Apr 2021 22:02:20 +0530 Subject: [PATCH] created multiprocess solution for mouse click callback for recording --- fishy/engine/fullautofisher/recorder.py | 7 ++-- fishy/helper/hotkey_process.py | 46 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 fishy/helper/hotkey_process.py diff --git a/fishy/engine/fullautofisher/recorder.py b/fishy/engine/fullautofisher/recorder.py index 9355817..279f69b 100644 --- a/fishy/engine/fullautofisher/recorder.py +++ b/fishy/engine/fullautofisher/recorder.py @@ -6,8 +6,8 @@ from tkinter.filedialog import asksaveasfile from fishy.engine.fullautofisher.engine import FullAuto, State -from fishy.helper import hotkey from fishy.helper.hotkey import Key +from fishy.helper.hotkey_process import HotKey class Recorder: @@ -35,7 +35,8 @@ class Recorder: def _start_recording(self): FullAuto.state = State.RECORDING logging.info("starting, press f8 to mark hole") - hotkey.set_hotkey(Recorder.mark_hole_key, self._mark_hole) + hk = HotKey() + hk.start_process(self._mark_hole) self.timeline = [] @@ -52,7 +53,7 @@ class Recorder: else: logging.warning("Took too much time to record") - hotkey.free_key(Recorder.mark_hole_key) + hk.stop() def func(): _file = None diff --git a/fishy/helper/hotkey_process.py b/fishy/helper/hotkey_process.py new file mode 100644 index 0000000..ad8313b --- /dev/null +++ b/fishy/helper/hotkey_process.py @@ -0,0 +1,46 @@ +import time +from threading import Thread + +import mouse +from multiprocessing import Process, Queue + + +def event_triggered(queue, e): + if not (type(e) == mouse.ButtonEvent and e.event_type == "up" and e.button == "left"): + return + + # call the parent function here + queue.put("left click") + + +def run(inq, outq): + mouse.hook(lambda e: event_triggered(outq, e)) + + stop = False + while not stop: + time.sleep(1) + if inq.get() == "stop": + stop = True + + +class HotKey: + def __init__(self): + self.inq = Queue() + self.outq = Queue() + + self.process = Process(target=run, args=(self.inq, self.outq)) + + def event_loop(self, func): + while True: + msg = self.outq.get() + if msg == "left click": + func() + + def start_process(self, func): + self.process.start() + Thread(target=self.event_loop, args=(func,)).start() + + def stop(self): + self.inq.put("stop") + self.process.join() + print("hotkey process ended")