moved hotkey to other process

This commit is contained in:
Adam Saudagar 2021-05-16 04:10:48 +05:30
parent 381f573109
commit 84b2b19b1a
8 changed files with 135 additions and 101 deletions

View File

@ -12,8 +12,8 @@ from fishy import gui, helper, web
from fishy.constants import chalutier, lam2 from fishy.constants import chalutier, lam2
from fishy.engine.common.event_handler import EngineEventHandler from fishy.engine.common.event_handler import EngineEventHandler
from fishy.gui import GUI, splash, update_dialog from fishy.gui import GUI, splash, update_dialog
from fishy.helper import hotkey
from fishy.helper.config import config from fishy.helper.config import config
from fishy.helper.hotkey.hotkey_process import hotkey
def check_window_name(title): def check_window_name(title):
@ -69,6 +69,8 @@ def initialize(window_to_hide):
def main(): def main():
config.init() config.init()
splash.start() splash.start()
hotkey.init()
print("launching please wait...") print("launching please wait...")
pil_logger = logging.getLogger('PIL') pil_logger = logging.getLogger('PIL')
@ -82,7 +84,7 @@ def main():
bot = EngineEventHandler(lambda: gui_window) bot = EngineEventHandler(lambda: gui_window)
gui_window = GUI(lambda: bot) gui_window = GUI(lambda: bot)
hotkey.initalize() hotkey.start()
logging.info(f"Fishybot v{fishy.__version__}") logging.info(f"Fishybot v{fishy.__version__}")
initialize(window_to_hide) initialize(window_to_hide)
@ -91,6 +93,7 @@ def main():
bot.start_event_handler() bot.start_event_handler()
config.stop() config.stop()
hotkey.stop()
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -4,7 +4,6 @@ import pickle
import time import time
import tkinter as tk import tkinter as tk
from tkinter import ttk from tkinter import ttk
from tkinter.messagebox import askyesno
from typing import List, Optional from typing import List, Optional
import typing import typing
from tkinter.filedialog import asksaveasfile from tkinter.filedialog import asksaveasfile
@ -13,6 +12,7 @@ from fishy.engine.fullautofisher.mode import player
from fishy.helper import helper from fishy.helper import helper
from fishy.helper.helper import empty_function, log_raise from fishy.helper.helper import empty_function, log_raise
from fishy.helper.hotkey.process import Key
from fishy.helper.popup import PopUp from fishy.helper.popup import PopUp
from playsound import playsound from playsound import playsound
@ -22,13 +22,11 @@ from fishy.helper.config import config
if typing.TYPE_CHECKING: if typing.TYPE_CHECKING:
from fishy.engine.fullautofisher.engine import FullAuto from fishy.engine.fullautofisher.engine import FullAuto
from fishy.engine.fullautofisher.mode.imode import IMode from fishy.engine.fullautofisher.mode.imode import IMode
from fishy.helper.hotkey import Key from fishy.helper.hotkey.hotkey_process import HotKey, hotkey
from fishy.helper.hotkey_process import HotKey
class Recorder(IMode): class Recorder(IMode):
recording_fps = 1 recording_fps = 1
mark_hole_key = Key.F8
def __init__(self, engine: 'FullAuto'): def __init__(self, engine: 'FullAuto'):
self.recording = False self.recording = False
@ -41,7 +39,6 @@ class Recorder(IMode):
logging.warning("QR not found, couldn't record hole") logging.warning("QR not found, couldn't record hole")
return return
self.timeline.append(("check_fish", coods)) self.timeline.append(("check_fish", coods))
playsound(helper.manifest_file("beep.wav"), False)
logging.info("check_fish") logging.info("check_fish")
def run(self): def run(self):
@ -58,8 +55,7 @@ class Recorder(IMode):
log_raise("QR not found") log_raise("QR not found")
logging.info("starting, press LMB to mark hole") logging.info("starting, press LMB to mark hole")
hk = HotKey() hotkey.hook(Key.LMB, self._mark_hole)
hk.start_process(self._mark_hole)
self.timeline = [] self.timeline = []
@ -78,7 +74,7 @@ class Recorder(IMode):
else: else:
logging.warning("Took too much time to record") logging.warning("Took too much time to record")
hk.stop() hotkey.free(Key.LMB)
if config.get("edit_recorder_mode"): if config.get("edit_recorder_mode"):
logging.info("moving to nearest coord in recording") logging.info("moving to nearest coord in recording")

View File

@ -7,13 +7,13 @@ import typing
from ttkthemes import ThemedTk from ttkthemes import ThemedTk
from fishy import helper from fishy import helper
from fishy.helper import hotkey
from fishy.web import web from fishy.web import web
from ..constants import chalutier, lam2 from ..constants import chalutier, lam2
from ..helper.config import config from ..helper.config import config
from ..helper.hotkey import Key
from .discord_login import discord_login from .discord_login import discord_login
from ..helper.hotkey.hotkey_process import hotkey
from ..helper.hotkey.process import Key
if typing.TYPE_CHECKING: if typing.TYPE_CHECKING:
from . import GUI from . import GUI
@ -126,7 +126,7 @@ def _create(gui: 'GUI'):
if config.get("win_loc") is not None: if config.get("win_loc") is not None:
gui._root.geometry(config.get("win_loc")) 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 # noinspection PyProtectedMember
def set_destroy(): def set_destroy():

View File

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

View File

View File

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

View File

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

View File

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