Answers for "python move mouse smoothly at constant speed"

0

python move mouse smoothly at constant speed

import pyautogui, math
from random import randint

def moveTo(x,y,speed=1100 , rand_x=0, rand_y=0, rand_time=0):
    """speed --> Pixels/sec 
    Check current cursor position and move relative to the distance to x,y input"""
    x_now, y_now = pyautogui.position()
    length = math.sqrt(pow(abs(x - x_now),2) + pow(abs(y - y_now),2))
    speed =  speed *0.6 if length < 500 else speed  # move slower when the cursor is close
    speed = 100 if speed <100 else speed    # limit minimum speed
    time = length  / speed + randint(-rand_time,rand_time)/1000
    print("distance: {}px  &  time: {}s".format(int(length), round(time,3) ) )
    pyautogui.moveTo(x+ randint(-rand_x,rand_x), 
                     y+ randint(-rand_y,rand_y), 
                     time, tween=pyautogui.easeOutQuad)

if __name__ == "__main__":
    moveTo(50,50)
Posted by: Guest on August-05-2020

Python Answers by Framework

Browse Popular Code Answers by Language