PyAutoGUi : How to know if the left mouse click is pressed
<p>Am using PyAutoGUI library, how to know if the left mouse click is pressed?</p>

<p>This is what I want to do : </p>

<pre><code>if(leftmousebuttonpressed):
   print("left")
else:
   print("nothing")
</code></pre>

<p>Help appreciated.</p>

<p>I don't think you can use PyAutoGui to listen for mouseclick.</p>

<p>Instead try Pyhook (from their source page):</p>

<pre><code>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()
</code></pre>

<p>I believe you can you do this:</p>

<pre><code>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()
</code></pre>

<p>They also do keyboards events, just go look their api up. </p>

<p>Edit:
Here's their mini tutorial: <a href="https://sourceforge.net/p/pyhook/wiki/PyHook_Tutorial/" rel="nofollow">https://sourceforge.net/p/pyhook/wiki/PyHook_Tutorial/</a></p>

<p>Also PyHook is only for windows (thanks to John Doe for pointing it out)</p>

python pyautogui