Python Global Capturing and Disposing Mouse Click Event

2k Views Asked by At

I want to create a tool, which allows me to send keys to active window using my mouse.

Let's say I want to send the key "A" when I click the left mouse button. But I also want to suppress / dispose that particular click event. So the target app will only feel the keyboard input "A", but not the left click of the mouse event.

With the following code I am able to see the mouse clicks. But I want to dispose / stop the event and not processed by the system.

from pynput import mouse

def do_something():
    print("something")

def on_click(x, y, button, pressed):
    print('{0} {1} at {2}'.format(button, 'Pressed' if pressed else 'Released', (x, y)))
    if (button.value == 1):
        do_something()
        #suppress / dispose the click event...

# Collect events until released
with mouse.Listener( on_click=on_click ) as listener:
    listener.join()

By the way I am using Ubuntu 20.04. Thanks in advance.

1

There are 1 best solutions below

0
podcast On

I found a way that suppress all the click events.

from pynput import mouse

def do_something():
    print("something")

def on_click(x, y, button, pressed):
    print('{0} {1} at {2}'.format(button, 'Pressed' if pressed else 'Released', (x, y)))
    if (button.value == 1):
        do_something()
        #suppress / dispose the click event...

# Collect events until released
with mouse.Listener( on_click=on_click, suppress=True ) as listener:
    listener.join()

I am still looking for a solution to suppress a certain button click.