通过完全控制,自动将鼠标从X1、Y1移动到X2、Y2

2024-09-29 17:20:04 发布

您现在位置:Python中文网/ 问答频道 /正文

这就是我想做的:

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

我正试图制作一个程序,在检查红场状况的同时做这个动作。如果它在路径中发现一个红方块,它将终止鼠标移动。因此,光标将位于红方块上

有些事。。。喜欢这:

Move Cursor(x1, y1)
While cursor isn't at finish point:
      Move Cursor(x2, y2)
      if red square:
        break

我不需要代码来检测红方块,但我需要有一个方法来移动鼠标,并有一个功能,可以突然终止鼠标移动

有什么想法吗


Tags: 路径程序move鼠标cursor方块x1起点
1条回答
网友
1楼 · 发布于 2024-09-29 17:20:04

好吧,让我们开始:
首先,您可以使用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

我愿意回答任何问题
我希望这有帮助,祝你好运

相关问题 更多 >

    热门问题