How to know if the left mouse click is pressed
Solution 1
(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.
Solution 2
How to know if left mouse click is pressed?
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
Monitoring other events (e.g. Mouse move, click, scroll)
See the code under Monitoring the mouse heading from original documentation. https://pynput.readthedocs.io/en/latest/mouse.html#monitoring-the-mouse
Solution 3
I don't think you can use PyAutoGui to listen for mouseclick.
Instead try Pyhook (from their source page):
import pythoncom, pyHook
def OnMouseEvent(event):
# called when mouse events are received
print 'MessageName:',event.MessageName
print 'Message:',event.Message
print 'Time:',event.Time
print 'Window:',event.Window
print 'WindowName:',event.WindowName
print 'Position:',event.Position
print 'Wheel:',event.Wheel
print 'Injected:',event.Injected
print '---'
# return True to pass the event to other handlers
return True
# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.MouseAll = OnMouseEvent
# set the hook
hm.HookMouse()
# wait forever
pythoncom.PumpMessages()
I believe you can you do this:
import pyHook, pythoncom
def left_down():
print("left down")
def right_down():
print("right down")
hm = pyHook.HookManager()
hm.SubscribeMouseLeftDown(left_down)
hm.SubscribeMouseRightDown(right_down)
hm.HookMouse()
pythoncom.PumpMessages()
hm.UnhookMouse()
They also do keyboards events, just go look their api up.
Edit: Here's their mini tutorial: https://sourceforge.net/p/pyhook/wiki/PyHook_Tutorial/
Also PyHook is only for windows (thanks to John Doe for pointing it out)
![Coding4Life](https://i.stack.imgur.com/KbcHG.png?s=256&g=1)
Comments
-
Coding4Life almost 2 years
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")
-
John Doe almost 8 yearsWould be nice if you provide a link to pyHook for quick access, and it is worth noting that pyhook runs only on windows.
-
Al Sweigart over 5 yearsUnfortunately, no. I'd recommend the pynput module at pypi.org/project/pynput for a module that can record mouse input.