[英]Automate Mouse Movement from X1,Y1 to X2,Y2 with complete control
This is what I'm trying to do:这就是我想要做的:
Move cursor from Start X, Y to Finish X, Y. In between the Start and Finish points, is a Red Square.将 cursor 从起点 X、Y 移动到终点 X、Y。在起点和终点之间,是一个红色方块。
I'm trying to make a program that does this movement while checking for the condition of the red square.我正在尝试制作一个程序来执行此动作,同时检查红色方块的状况。 If it finds a red square in its path, it will terminate the mouse movement.如果它在其路径中发现一个红色方块,它将终止鼠标移动。 So the cursor will be on the red square.所以 cursor 将在红色方块上。
Something... like... This:一些东西......就像......这个:
Move Cursor(x1, y1)
While cursor isn't at finish point:
Move Cursor(x2, y2)
if red square:
break
I do not need the code to detect the red square, but I need to have a method to move the mouse and have a feature that can abruptly terminate the mouse movement.我不需要代码来检测红色方块,但我需要有一个移动鼠标的方法,并有一个可以突然终止鼠标移动的功能。
Any Ideas?有任何想法吗?
Well, Lets Start This:好吧,让我们开始吧:
first, you could use pyinput , its a realiable library I have used so many times to Control mouse and keyboard, read Here: Pyinput首先,您可以使用pyinput ,它是一个我用过很多次来控制鼠标和键盘的可靠库,请阅读此处: Pyinput
Second, look at my line by line detailed Example Below:Your Code will look something like it二、看我的逐行详细示例如下:你的代码会是这样的
from pynput.mouse import Button, Controller # importing the Function
mouse = Controller() # getting the mouse controller
########################################################################## The function you need
def moveCursor( # the Function name is not representable, personally I would have named it GlideMouseUntil()
x1,y1, #the Start Position. type (int)
x2,y2, #the End Position. type (int)
intervals, #How many points on path you want to check. type (int)
CheckerFunction #this is the function that will check for the red Square, must return True to stop, False means continue. type(func name)
):
mouse.position = (x1,y1) #set the inital mouse position to the start position
distance_x = x2-x1 #calculate the Horizontal distance between the two points
distance_y = y2-y1 #calculate the Vertical distance between the two points
for n in range(0, intervals+1): #for Every point on the line
if CheckerFunction(): #Run the ckecker function
break #if it returns True: break from the loop and exit the function , Red square Found !! YaY
else: #if it returns False
mouse.move(x1 + n * (distance_x/intervals), y1 + n * (distance_y/intervals)) #Calulate the Next position and go to it
pass
pass
##########################################################################
def checkForRedSquare(): # The function that will Check for the red Square, must return True if square is found . false if not
if SquareIsFound:
return True
pass
else:
return False
pass
##########################################################################
moveCursor(10,10,1000,1000, 30,checkForRedSquare) # means check 30 equally distanced point from poosition(10,10) until (1000,1000) if Square is Found in between stop
I am open to Any Questions我愿意接受任何问题
I hope that Helps, good Luck!!我希望有帮助,祝你好运!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.