I am using PyAutoGUI library. How can I know if the left mouse button is pressed?
This is what I want to do:
if(leftmousebuttonpressed):
print("left")
else:
print("nothing")
(I'm the author of PyAutoGUI.) I can confirm that currently PyAutoGUI can't read/record clicks or keystrokes. These features are on the roadmap, but there aren't any resources or timelines dedicated to them currently.
A simple version of the code inspired from the original documentation:
from pynput import mouse
def on_click(x, y, button, pressed):
if button == mouse.Button.left:
print('{} at {}'.format('Pressed Left Click' if pressed else 'Released Left Click', (x, y)))
return False # Returning False if you need to stop the program when Left clicked.
else:
print('{} at {}'.format('Pressed Right Click' if pressed else 'Released Right Click', (x, y)))
listener = mouse.Listener(on_click=on_click)
listener.start()
listener.join()
Like Sir Al Sweigart mentioned in the comment above, I looked for pynput module which works perfect. See documentation and PyPi instructions at:
Install the library using pip: pip install pynput
See the code under Monitoring the mouse heading from original documentation. https://pynput.readthedocs.io/en/latest/mouse.html#monitoring-the-mouse
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With