Simulate a mouse click / Define color under cursor in Python

I am very new to python. I am trying to write a program that clicks on (x, y), moves it to (a, b), and then waits until the color under the mouse has a specific color, say #fff. When it is that color, it will click again and then repeat.

I cannot find a good API for mouse related files for python.

+5
source share
2 answers

The API for modeling mouse events depends on your platform. I do not know a cross-platform solution.

On Windows, you can access the Win32 API thanks to ctypes. see mouse_event on MSDN . You May Also Want Pywinauto

. . GetCursorPos MSDN. , API , . , PIL . , PIL Windows paltform, .

:

def grab_main_color(self, rect, max_colors=256):
    """returns a tuple with the RGB value of the most present color in the given rect"""
    img=ImageGrab.grab(rect)
    colors = img.getcolors(max_colors)
    max_occurence, most_present = 0, 0
    try:
        for c in colors:
            if c[0] > max_occurence:
                (max_occurence, most_present) = c
        return most_present
    except TypeError:
        raise Exception("Too many colors in the given rect")
+6

Windows, autohotkey. python, Windows. . " ".

-1