moses-palmer / pynput

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

How can I suppress/disable the left click or right click, or any specific key #603

Open DollieSon opened 4 months ago

DollieSon commented 4 months ago

I've tried following the faq but I dont understand what the 'listener' stood for and yes i've tried using 'self.' here is the closest thing i've come so far:

` def test3(): def win32_event_filter(msg,data): if msg != 512: print(data.mouseData) print("Something was presed")

semething = mouse.Listener(win32_event_filter=win32_event_filter)
semething.start()
print("Mouse and keyboard events disabled")
time.sleep(5)
semething.stop()

`

Ananym commented 4 months ago

The docs aren't the best on this, I agree. What the docs are saying is that you can call suppress_event on your listener during the win32_event_filter function to suppress the event (this will not then trigger the other hooks on the listener)

What I've found is that with the mouse listener on windows in particular, you can call suppress_event in the other hooks too. For instance, this works fine:

mouse_listener = mouse.Listener()
def on_scroll(x,y,dx,dy):
    mouse_listener.suppress_event()
mouse_listener.on_scroll = on_scroll

def on_click(x,y,button,pressed):
    if button == mouse.Button.right:
        mouse_listener.suppress_event()
mouse_listener.on_click = on_click

This pattern doesn't work for me with the keyboard. Might open another issue for that.