How to move the mouse in a realistic way using Python?

4

How to make the mouse move automatically, realistically, using Python?

I was able to find and change a code that produces a "V" -shaped movement on the screen. However, I would like to move the mouse using non-linear movements.

I found some interesting ideas:

  • Algorithm to mimic the movement of the mouse as a human
  • Moving the mouse realistically using C #
  • Moving the mouse realistically using a Spline from # or Interpolation Approach
  • I'm having a hard time incorporating the mathematical part of the nonlinearity of the movement into my code.

    Code:

    import sys
    import time
    import win32api
    
    if (len(sys.argv) < 4):
        print "Como utilizar: python mousemove.py dx dy speed"
        sys.exit()
    
    current = win32api.GetCursorPos()
    cx = sx = current[0]
    cy = sy = current[1]
    
    mx = int(sys.argv[1])
    my = int(sys.argv[2])
    vx = vy = int(sys.argv[3])
    
    print "Movendo", mx, my, "com", vx, "pixels por segundo"
    print "Pressione 'q' para sair"
    
    last = time.time()
    
    while(True):
        if win32api.GetAsyncKeyState(ord('Q')):
            sys.exit()
    
        current = time.time()
        tick = current - last
        last = current
    
        if mx > 0:
            cx += vx * tick;
            if cx > mx + sx or cx < sx:
                vx = -vx;
                cx = max( sx, min( mx + sx, cx ) )
        if( my > 0 ):
            cy += vy * tick;
            if cy > my + sy or cy < sy:
                vy = -vy;
                cy = max( sy, min( my + sy, cy ) )
    
        win32api.SetCursorPos((int(cx),int(cy)))
        time.sleep(0.001)
    

    Mathematical excerpt that draws a V:

    if mx > 0:
        cx += vx * tick;
        if cx > mx + sx or cx < sx:
            vx = -vx;
            cx = max( sx, min( mx + sx, cx ) )
    if( my > 0 ):
        cy += vy * tick;
        if cy > my + sy or cy < sy:
            vy = -vy;
            cy = max( sy, min( my + sy, cy ) )
    

    Question:

    How do I set the conditions to incorporate a certain randomness (non-linearity) in the movement of the mouse cursor?

        
    asked by anonymous 12.01.2017 / 18:53

    1 answer

    4

    Well interesting the problem. I believe that a good technique to improve would be to use the concepts already studied in UX ( User Experience ). For example, as quoted in the Google Material , you can use the Arc Upward or Arc Downward < strong>, by checking the difference of the initial and final desired positions.

    If the end position of the mouse is above the starting position, use the arc upward movement.

    Iftheendpositionofthemouseisbelowthestartingposition,usethearcdownwardmovement.

    Themovementsseemmuchmorenaturalthanthelinear.

      

    Note:Youcanreadabitofmath here / a>. I believe that the implementation of the equations is not complex. I will try to do it soon and, if it works, edit the response with the codes.

        
    19.02.2017 / 17:04