[英]Automate Mouse Movement from X1,Y1 to X2,Y2 with complete control
这就是我想要做的:
将 cursor 从起点 X、Y 移动到终点 X、Y。在起点和终点之间,是一个红色方块。
我正在尝试制作一个程序来执行此动作,同时检查红色方块的状况。 如果它在其路径中发现一个红色方块,它将终止鼠标移动。 所以 cursor 将在红色方块上。
一些东西......就像......这个:
Move Cursor(x1, y1)
While cursor isn't at finish point:
Move Cursor(x2, y2)
if red square:
break
我不需要代码来检测红色方块,但我需要有一个移动鼠标的方法,并有一个可以突然终止鼠标移动的功能。
有任何想法吗?
好吧,让我们开始吧:
首先,您可以使用pyinput ,它是一个我用过很多次来控制鼠标和键盘的可靠库,请阅读此处: Pyinput
二、看我的逐行详细示例如下:你的代码会是这样的
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
我愿意接受任何问题
我希望有帮助,祝你好运!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.