'How to know if the left mouse click is pressed

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")


Solution 1:[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:[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:[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)

Solution 4:[4]

you can use win32api which is a built-in library as in the following example:

import win32api

if win32api.GetKeyState(0x01)<0: #if mouse left button is pressed
   #do something
else: #if mouse left button is not pressed
   #do something

as for why that code functions like that here's the explanation:

you do "<0" because the GetKeyState function returns either a negative number or 1 or 0, 1 or 0 are for when the button is released and it changes each time u let go and the negative number is for when the button is pressed, as an example if the mouse button is now returning a 0 using the GetKeyState function and you press it it will return a negative number until u let go and then it will return 1, and if it was a 1 and u press the button it will return a negative number while your holding it down and then once u let go it will go back to being 0 (you can print(GetKeyState(0x01) and see for yourself for a better understanding), as for "0x01" thats the virtual key code for the left mouse button and you can find all the virtual keycodes of each and every button on the mouse and the keyboard using this site provided by microsoft themselves https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3
Solution 4 tawfik khetib