created multiprocess solution for mouse click callback for recording

This commit is contained in:
Adam Saudagar 2021-04-17 22:02:20 +05:30
parent 96db413f61
commit babcdd262a
2 changed files with 50 additions and 3 deletions

View File

@ -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

View File

@ -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")