在完全控制的情况下,自动将鼠标从X1、Y1移动到X2、Y2



这就是我要做的:

将光标从起点X,Y移动到终点X,Y。在起点和终点之间是一个红色方块。

我正在尝试制作一个程序,在检查红色方块的条件的同时完成这个动作。如果它在路径中发现一个红色方块,它将终止鼠标移动。因此光标将位于红色方块上。

什么。。。喜欢此:

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

我对任何问题都持开放态度
我希望这会有所帮助,祝你好运!!

最新更新