Search code examples
pythonpython-2.7autopy

simulate cursor motion from X,Y to X,Y


I need to move mouse cursor from coordinates (800,300) to (100,600) with visible cursor movement. How can I do that? (I need to simulate motion only - I am getting mouse position with autopy module)


Solution

  • Directly from the docs:

    import autopy
    
    autopy.mouse.move(800, 300)
    autopy.mouse.smooth_move(100, 600)
    

    This first moves to the location and then linearly slides the mouse to the second location. With a combination of pauses, you can use autopy.mouse.move to move as slow or as fast as you want.

    Edit by request: To a finer control over the smooth_move you can set the mouse position yourself. Here, I set the total_time to be 5.00 seconds, but you can change this to be as quick as you like.

    from __future__ import division
    import autopy
    import time
    
    x0, y0 = 800, 300
    xf, yf = 100, 600
    
    total_time = 5.00  # in seconds
    draw_steps = 1000  # total times to update cursor
    
    dx = (xf-x0)/draw_steps
    dy = (yf-y0)/draw_steps
    dt = total_time/draw_steps
    
    for n in xrange(draw_steps):
        x = int(x0+dx*n)
        y = int(y0+dy*n)
        autopy.mouse.move(x,y)
        time.sleep(dt)