click on coordinates by left mouse in python

To click on specific coordinates by the left mouse button in Python, we can use the pynput library, which allows us to control and monitor input devices like a mouse or keyboard.

Here is an example code to click on coordinates (x,y) using the left mouse button:

main.py
from pynput.mouse import Button, Controller

# Create an instance of the mouse controller
mouse = Controller()

# Set the coordinates of the point to click
x = 100
y = 200

# Move the mouse pointer to the desired coordinates
mouse.position = (x, y)

# Click the left button once
mouse.press(Button.left)
mouse.release(Button.left)
331 chars
16 lines

In the above code, we first import the Button and Controller classes from the pynput.mouse module. Then, we create a Controller object to control the mouse.

Next, we set the coordinates of the point where we want to click by assigning values to the x and y variables.

After that, we move the mouse pointer to the desired coordinates by setting the position attribute of the mouse object to a tuple of the (x, y) values.

Finally, we issue the "click" by pressing the left button down ("press") and releasing it immediately after ("release").

gistlibby LogSnag