How to detect a mouse circling around something in python?

40 Views Asked by At

I'm trying to detect when a user's mouse is circling around an object repeatedly, and take a screenshot of that object.

To make this simpler, I'll split it into two parts. First of all, I want to detect the mouse circling. This is easy, there are many python libraries for this. But I don't want the screenshot to be taken by accident, so I want the user's mouse to be circling for at least 5 seconds before we take the screenshot. For example:

#import necessary libraries

def get_mouse_input():
    pass

def get_coordinates():
    pass

When I say get_coordinates(), I mean that I want to get the coordinates of the area that the mouse is circling around. That way, I can get a screenshot of that particular area.

Next, taking the screenshot, which is pretty straightforward, you simply just use screenshot module and perhaps find a way to crop the image, and you've got it.

The real problem for me is detecting when the user is circling around an area. I don't care if the user is circling around white space, or black space, what I care about is what the coordinates of the mouse are. The problem is, the mouse kind of makes the shape of a circle or ellipse when you circle it, and of course, user input can vary, so it makes it extremely hard to detect something like this.

The main question I'm asking here is: Is this possible to do in python? And if so, how? This is just a fun project of mine, so I'm open to any suggestions.

UPDATE:

I've figured out how to get the mouse input, but the problem lies with using that input. It's easy to get the mouse position, but actually seeing if those coordinates lie in the shape of an ellipse or circle is difficult. For example, let's say that I have 4 coordinates, which the mouse repeatedly is touching:

import pyautogui
import time

while True:
    cordslist1 = []
    cordslist2 = []
    cordslist3 = []
    cordslist4 = []
    for i in range(4):
        cords1 = pyautogui.position()
        time.sleep(0.5)
        cords2 = pyautogui.position()
        time.sleep(0.5)
        cords3 = pyautogui.position()
        time.sleep(0.5)
        cords4 = pyautogui.position()
        for j in range(4):
            exec(f"cordslist{i + 1}.append(cords{j + 1}")
    cordslist = [cordslist1, cordslist2, cordslist3, cordslist4]
    #do something with cordslist

and...now I have a huge list called cordslist, which was achieved using some questionable methods...and I don't know what to do with it. For example, say I have 4 coordinates: (10, 4), (100, 100), (200, 200), and (7, 8). How do I detect if that shape is an ellipse or circle? Then, how do I take a screenshot of those coordinates as well? I'm still a little stuck. I'm hoping there is a mathematical function that might help me with this, using the python module math, or just some basic for loops.

UPDATE:

I've solved this. I've found a much simpler solution than detecting the coordinates each time (using a python library) that works perfectly. To anybody else who tries this, know that doing it the way I tried to do it from the start is NOT a good idea.

0

There are 0 best solutions below