python move mouse smoothly at constant speed

Solutions on MaxInterview for python move mouse smoothly at constant speed by the best coders in the world

showing results for - "python move mouse smoothly at constant speed"
Page
18 Aug 2018
1import pyautogui, math
2from random import randint
3
4def moveTo(x,y,speed=1100 , rand_x=0, rand_y=0, rand_time=0):
5    """speed --> Pixels/sec 
6    Check current cursor position and move relative to the distance to x,y input"""
7    x_now, y_now = pyautogui.position()
8    length = math.sqrt(pow(abs(x - x_now),2) + pow(abs(y - y_now),2))
9    speed =  speed *0.6 if length < 500 else speed  # move slower when the cursor is close
10    speed = 100 if speed <100 else speed    # limit minimum speed
11    time = length  / speed + randint(-rand_time,rand_time)/1000
12    print("distance: {}px  &  time: {}s".format(int(length), round(time,3) ) )
13    pyautogui.moveTo(x+ randint(-rand_x,rand_x), 
14                     y+ randint(-rand_y,rand_y), 
15                     time, tween=pyautogui.easeOutQuad)
16
17if __name__ == "__main__":
18    moveTo(50,50)
similar questions