From 84b2b19b1ac9da93a71eac62e888ac4a08f96266 Mon Sep 17 00:00:00 2001 From: Adam Saudagar Date: Sun, 16 May 2021 04:10:48 +0530 Subject: [PATCH] moved hotkey to other process --- fishy/__main__.py | 7 +- fishy/engine/fullautofisher/mode/recorder.py | 12 +-- fishy/gui/main_gui.py | 6 +- fishy/helper/hotkey.py | 42 ----------- fishy/helper/hotkey/__init__.py | 0 fishy/helper/hotkey/hotkey_process.py | 78 ++++++++++++++++++++ fishy/helper/hotkey/process.py | 45 +++++++++++ fishy/helper/hotkey_process.py | 46 ------------ 8 files changed, 135 insertions(+), 101 deletions(-) delete mode 100644 fishy/helper/hotkey.py create mode 100644 fishy/helper/hotkey/__init__.py create mode 100644 fishy/helper/hotkey/hotkey_process.py create mode 100644 fishy/helper/hotkey/process.py delete mode 100644 fishy/helper/hotkey_process.py diff --git a/fishy/__main__.py b/fishy/__main__.py index 738bf9a..0f73099 100644 --- a/fishy/__main__.py +++ b/fishy/__main__.py @@ -12,8 +12,8 @@ from fishy import gui, helper, web from fishy.constants import chalutier, lam2 from fishy.engine.common.event_handler import EngineEventHandler from fishy.gui import GUI, splash, update_dialog -from fishy.helper import hotkey from fishy.helper.config import config +from fishy.helper.hotkey.hotkey_process import hotkey def check_window_name(title): @@ -69,6 +69,8 @@ def initialize(window_to_hide): def main(): config.init() splash.start() + hotkey.init() + print("launching please wait...") pil_logger = logging.getLogger('PIL') @@ -82,7 +84,7 @@ def main(): bot = EngineEventHandler(lambda: gui_window) gui_window = GUI(lambda: bot) - hotkey.initalize() + hotkey.start() logging.info(f"Fishybot v{fishy.__version__}") initialize(window_to_hide) @@ -91,6 +93,7 @@ def main(): bot.start_event_handler() config.stop() + hotkey.stop() if __name__ == "__main__": diff --git a/fishy/engine/fullautofisher/mode/recorder.py b/fishy/engine/fullautofisher/mode/recorder.py index 8acdbf4..040ac6d 100644 --- a/fishy/engine/fullautofisher/mode/recorder.py +++ b/fishy/engine/fullautofisher/mode/recorder.py @@ -4,7 +4,6 @@ import pickle import time import tkinter as tk from tkinter import ttk -from tkinter.messagebox import askyesno from typing import List, Optional import typing from tkinter.filedialog import asksaveasfile @@ -13,6 +12,7 @@ from fishy.engine.fullautofisher.mode import player from fishy.helper import helper from fishy.helper.helper import empty_function, log_raise +from fishy.helper.hotkey.process import Key from fishy.helper.popup import PopUp from playsound import playsound @@ -22,13 +22,11 @@ from fishy.helper.config import config if typing.TYPE_CHECKING: from fishy.engine.fullautofisher.engine import FullAuto from fishy.engine.fullautofisher.mode.imode import IMode -from fishy.helper.hotkey import Key -from fishy.helper.hotkey_process import HotKey +from fishy.helper.hotkey.hotkey_process import HotKey, hotkey class Recorder(IMode): recording_fps = 1 - mark_hole_key = Key.F8 def __init__(self, engine: 'FullAuto'): self.recording = False @@ -41,7 +39,6 @@ class Recorder(IMode): logging.warning("QR not found, couldn't record hole") return self.timeline.append(("check_fish", coods)) - playsound(helper.manifest_file("beep.wav"), False) logging.info("check_fish") def run(self): @@ -58,8 +55,7 @@ class Recorder(IMode): log_raise("QR not found") logging.info("starting, press LMB to mark hole") - hk = HotKey() - hk.start_process(self._mark_hole) + hotkey.hook(Key.LMB, self._mark_hole) self.timeline = [] @@ -78,7 +74,7 @@ class Recorder(IMode): else: logging.warning("Took too much time to record") - hk.stop() + hotkey.free(Key.LMB) if config.get("edit_recorder_mode"): logging.info("moving to nearest coord in recording") diff --git a/fishy/gui/main_gui.py b/fishy/gui/main_gui.py index 7c74c84..cc847ae 100644 --- a/fishy/gui/main_gui.py +++ b/fishy/gui/main_gui.py @@ -7,13 +7,13 @@ import typing from ttkthemes import ThemedTk from fishy import helper -from fishy.helper import hotkey from fishy.web import web from ..constants import chalutier, lam2 from ..helper.config import config -from ..helper.hotkey import Key from .discord_login import discord_login +from ..helper.hotkey.hotkey_process import hotkey +from ..helper.hotkey.process import Key if typing.TYPE_CHECKING: from . import GUI @@ -126,7 +126,7 @@ def _create(gui: 'GUI'): if config.get("win_loc") is not None: gui._root.geometry(config.get("win_loc")) - hotkey.set_hotkey(Key.F9, gui.funcs.start_engine) + hotkey.hook(Key.F9, gui.funcs.start_engine) # noinspection PyProtectedMember def set_destroy(): diff --git a/fishy/helper/hotkey.py b/fishy/helper/hotkey.py deleted file mode 100644 index 7eedbdc..0000000 --- a/fishy/helper/hotkey.py +++ /dev/null @@ -1,42 +0,0 @@ -from enum import Enum -from threading import Thread -from typing import Callable, Dict, Optional - -import keyboard - - -class Key(Enum): - F9 = "f9" - F10 = "f10" - F8 = "f8" - F7 = "f7" - UP = "up" - DOWN = "down" - LEFT = "left" - RIGHT = "right" - - -_hotkeys: Dict[Key, Optional[Callable]] = {} - - -def _get_callback(k): - def callback(): - if not _hotkeys[k]: - return - - Thread(target=_hotkeys[k]).start() - return callback - - -def initalize(): - for k in Key: - _hotkeys[k] = None - keyboard.add_hotkey(k.value, _get_callback(k)) - - -def set_hotkey(key: Key, func: Optional[Callable]): - _hotkeys[key] = func - - -def free_key(k: Key): - set_hotkey(k, None) diff --git a/fishy/helper/hotkey/__init__.py b/fishy/helper/hotkey/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/fishy/helper/hotkey/hotkey_process.py b/fishy/helper/hotkey/hotkey_process.py new file mode 100644 index 0000000..e82efed --- /dev/null +++ b/fishy/helper/hotkey/hotkey_process.py @@ -0,0 +1,78 @@ +import time +from multiprocessing import Process, Queue +from threading import Thread +from typing import Dict, Optional, Callable + +from playsound import playsound + +from fishy import helper +from fishy.helper.hotkey import process +from fishy.helper.hotkey.process import Key + + +# noinspection PyPep8Naming +class hotkey: + instance: 'HotKey' = None + + @staticmethod + def init(): + if not hotkey.instance: + hotkey.instance = HotKey() + + @staticmethod + def hook(key: Key, func: Callable): + hotkey.instance.hook(key, func) + + @staticmethod + def free(key: Key): + hotkey.instance.free(key) + + @staticmethod + def start(): + hotkey.instance.start() + + @staticmethod + def stop(): + hotkey.instance.stop() + + +class HotKey: + def __init__(self): + self.inq = Queue() + self.outq = Queue() + + self._hotkeys: Dict[Key, Optional[Callable]] = dict([(k, None) for k in Key]) + + self.process = Process(target=process.run, args=(self.inq, self.outq)) + self.event = Thread(target=self._event_loop) + + def hook(self, key: Key, func: Callable): + self._hotkeys[key] = func + + def free(self, key: Key): + self._hotkeys[key] = None + + def _event_loop(self): + while True: + key = self.outq.get() + + if key in Key: + callback = self._hotkeys[key] + if callback: + playsound(helper.manifest_file("beep.wav"), False) + callback() + elif key == "stop": + break + + time.sleep(0.1) + + def start(self): + self.process.start() + self.event.start() + + def stop(self): + self.inq.put("stop") + self.outq.put("stop") + self.process.join() + self.event.join() + print("hotkey process ended") diff --git a/fishy/helper/hotkey/process.py b/fishy/helper/hotkey/process.py new file mode 100644 index 0000000..0e63e31 --- /dev/null +++ b/fishy/helper/hotkey/process.py @@ -0,0 +1,45 @@ +import time +from enum import Enum + +import keyboard +import mouse + + +class Key(Enum): + F9 = "f9" + LMB = "left" + + +mouse_buttons = [Key.LMB] + + +def _mouse_callback(queue): + def callback(e): + # noinspection PyProtectedMember + if not (type(e) == mouse.ButtonEvent and e.event_type == "up" and e.button in Key._value2member_map_): + return + + # call the parent function here + queue.put(Key(e.button)) + + return callback + + +def _keyboard_callback(queue, k): + def callback(): + queue.put(k) + + return callback + + +def run(inq, outq): + mouse.hook(_mouse_callback(outq)) + for k in Key: + if k not in mouse_buttons: + keyboard.add_hotkey(k.value, _keyboard_callback(outq, k)) + + stop = False + while not stop: + if inq.get() == "stop": + stop = True + time.sleep(1) diff --git a/fishy/helper/hotkey_process.py b/fishy/helper/hotkey_process.py deleted file mode 100644 index a4d5d0e..0000000 --- a/fishy/helper/hotkey_process.py +++ /dev/null @@ -1,46 +0,0 @@ -import time -from multiprocessing import Process, Queue -from threading import Thread - -import mouse - - -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")