moses-palmer / pynput

Sends virtual input commands
GNU Lesser General Public License v3.0
1.73k stars 243 forks source link

pynput doing mouse click action inside on_click #572

Closed Kelv1nG closed 9 months ago

Kelv1nG commented 9 months ago

im having a problem using mouse on_click function of pynput whenever the supplied function also performs a mouse click

quite similar with this post Taking Single Click Event and Propagating it to Multiple Areas - python, pynput, pyautogui

but i cant seem to make it work it just freezes

def win32_event_filter(msg, data):
    if data.flags:
        print('suppressing event')
        return False

class EventHandler:
    def __init__(self):
        self.mouse_listener = None

    def start(self):
        self.mouse_listener = mouse.Listener(on_click=self.on_click, win32_event_filter=win32_event_filter)
        self.mouse_listener.start()
        self.mouse_listener.join()

    def on_click(self, x, y, button, pressed):
        if pressed:
            if button == Button.left:
                self.do_action()
            if button == Button.right:
                self.perform_click()

    def do_action(self):
        print('doing action')
        print('action done')

    def perform_click(self):
        controller.click(Button.left) # crashes

is this a known issue or am i just doing it wrong? im using python 3.10, data.flags when using controller.click = 1, user click = 0

Kelv1nG commented 9 months ago

i managed to make it work by passing the perform click to a separate thread as stated in https://github.com/hieupc20122009/pynput/blob/main/The%20mouse%20listener%20thread.py