如何使用python实现鼠标画圈移动

问题描述 投票:0回答:3

我正在尝试用 python 编写一个脚本,在没有用户输入的情况下自动强制移动鼠标指针(它通过键盘退出),并尝试使用 PyAutoGUI、PyUserInput 和 ctypes,我一直在想办法以恒定速度移动指针,而不是让它在屏幕上瞬移(我需要用户能够看到它所走的路径)。但是,我需要它能够执行曲线,尤其是圆,而且我还没有找到使用上述库执行此操作的方法。有谁知道一种方法可以将它们编码为使鼠标以恒定速度在屏幕上画圆,而不仅仅是直线?预先感谢您提供的任何意见或帮助。

python mouse ctypes mousemove pyautogui
3个回答
5
投票

这是我尝试在半径为 R 的屏幕中心制作圆圈 - 还要注意,如果我不传递参数 duration,鼠标指针会立即移动到下一个坐标。因此,对于分成 360 个部分的圆,您可以使用模数来设置速度。

import pyautogui
import math

# Radius 
R = 400
# measuring screen size
(x,y) = pyautogui.size()
# locating center of the screen 
(X,Y) = pyautogui.position(x/2,y/2)
# offsetting by radius 
pyautogui.moveTo(X+R,Y)

for i in range(360):
    # setting pace with a modulus 
    if i%6==0:
       pyautogui.moveTo(X+R*math.cos(math.radians(i)),Y+R*math.sin(math.radians(i)))

0
投票

有一种方法可以使用 sin、cos 和 tan 来做到这一点。 (我还没有能够测试这段代码,它可能行不通。)

Import math
Import pyautogui
def circle(radius = 5, accuracy = 360, xpos=0, ypos=0, speed = 5):
    local y
    local x
    local angle
    angle = 360/accuracy
    local CurAngle
    CurAngle = 0
    x = []
    y = []
    sped = speed/accuracy
    for i in range(accuracy):
        x.append(xpos + radius*math.sin(math.radians(CurAngle)))
        y.append(ypos + radius*math.cos(math.radians(CurAngle)))
        CurAngle += angle
    for i in len(x):
        pyautogui.moveTo(x[i], y[i], duration = sped)

你把它放在脚本的顶部附近,并像这样传递参数:
圆(半径、精度、xpos、ypos、速度)
半径控制圆的宽度
accuracy 控制圆被分成多少个等距点,将 accuracy 设置为 4 将沿着圆放置 4 个不可见的点供鼠标移动,这将形成一个正方形,而不是圆形,5 形成一个五边形, 6 六边形等。半径越大,你想要的精度就越大
Xpos 控制圆心的 x 位置
Ypos 控制圆心的 y 位置
速度控制您希望绘制圆圈所需的秒数。 希望这会有所帮助 :) 当你说“曲线”时,你介意详细说明你想要什么吗


0
投票
import pyautogui as pg

resolution = pg.size()
radius = 300 # Circle radius
duration = 0.1 # Duration of each move(mouse speed)
steps = 20 # Number of 'moves' or points on the circle

mid_x = resolution[0] / 2
mid_y = resolution[1] / 2

def GetXnY(angle):
    x = radius * math.sin(math.pi * 2 * angle / 360)
    y = radius * math.cos(math.pi * 2 * angle / 360)
    return (x, y)

def DoACircle():
    angle = 0
    while angle  <= 360:
        x, y = GetXnY(angle)
        pg.moveTo((mid_x + x), (mid_y - y), duration = duration)
        angle = angle + (360/steps)
           
DoACircle()
© www.soinside.com 2019 - 2024. All rights reserved.