ILikeAI / AlwaysReddy

AlwaysReddy is a LLM voice assistant that is always just a hotkey away.
MIT License
524 stars 50 forks source link

Add mouse hotkey functionality with pynput #17

Open daermond opened 2 months ago

daermond commented 2 months ago

pyinput give the possibility to add mouse hotkey, like the side buttons. It is much convenient to use it with a thumb click. Could you elaborate it to add mouse hotkey functionality as well with pynput? It can be used next to the keyboard hotkeys. I have done it personally to test it and works great. Like:

from pynput import mouse

    def run(self):
        """Run the recorder, setting up hotkeys and entering the main loop."""
        keyboard.add_hotkey(config.RECORD_HOTKEY, self.handle_hotkey_wrapper)
        keyboard.add_hotkey(config.CANCEL_HOTKEY, self.cancel_all)
        keyboard.add_hotkey(config.CLEAR_HISTORY_HOTKEY, self.clear_messages)
        def on_click(x, y, button, pressed):
            if button == getattr(mouse.Button, config.MOUSE_CANCEL_HOTKEY) and pressed:
                self.cancel_all()
            elif button == getattr(mouse.Button, config.MOUSE_RECORD_HOTKEY) and pressed:
                self.handle_hotkey_wrapper()
            elif button == getattr(mouse.Button, config.MOUSE_CLEAR_HISTORY_HOTKEY) and pressed:
                self.clear_messages()
        mouse_listener = mouse.Listener(on_click=on_click)
        mouse_listener.start()
        print(f"Press '{config.RECORD_HOTKEY}' to start recording, press again to stop and transcribe.\nDouble tap to give the AI access to read your clipboard.\nPress '{config.CANCEL_HOTKEY}' to cancel recording.\nPress '{config.CLEAR_HISTORY_HOTKEY}' to clear the chat history.")

Or replace the keyboard as well with pynput as in a pull request suggested.

Config.py:

### HOTKEYS ###
CANCEL_HOTKEY = 'ctrl + alt + x'
CLEAR_HISTORY_HOTKEY = 'ctrl + alt + f12'
RECORD_HOTKEY = 'ctrl + shift + space'
# Mouse keys are:'x1' mouse sidebutton 1, 'x2' for mouse sidebutton 2, 'middle' for mouse middle button
MOUSE_CANCEL_HOTKEY = 'x1'
MOUSE_CLEAR_HISTORY_HOTKEY = 'middle'
MOUSE_RECORD_HOTKEY = 'x2'
ILikeAI commented 2 months ago

Oh awesome having it hooked up to a mouse button would be awesome! I'm looking at swapping away from the current keyboard library, at very least I will add support for pynput. Thanks for submitting the issue!