pythonpywin32win32com

How to slow down mouse speed when using win32api


I'm attempting to make my mouse slowly go across my screen but have had trouble finding a way to slow down the mouse speed. Someone posted a solution to an issue 11 months ago the solution they posted isn't working for me or I am using it wrong.

Possible Solution: How to change the mouse movement speed with win32api (mouseeventf_move)

def farm():
    #win32api.SetCursorPos((0,1440))
    win32api.mou0se_event(win32con.MOUSEEVENTF_MOVE, int(100) * 2, int(100) * 2, 0, 0)

    time.sleep(0.5)

    win32api.mouse_event(win32con.MOUSEEVENTF_MOVE, int(1) * 5, int(3) * 7, 0, 0)
    #win32api.SetCursorPos((2560,1000))

So far I have attempted the code in the snippet above. The uncommented code is the attempted solution that I tried using from another post. This moves the mouse in the x and y direction of the first two numbers that are multiplied by a smoothing factor. I haven't found a way to slow down the speed with this method. If there is a way to do it while using SetCursorPos that would be great but if not I can understand why.

Edit: Removed the image and added a snippet, didn't know about not uploading images my bad.


Solution

  • I did quite a lot of digging, and unfortunately, I couldn't find a way to make win32api work.  

    I would recommend using pyautogui instead of win32api. Pyautogui is a GUI automation module, that can control the mouse and keyboard, as well as find images on screen and take screenshots using Pillow.

    Pyautogui has the moveTo() and move() functions that should solve your problem. For example:

    import pyautogui
    
    def farm():
        pyautogui.moveTo(100, 100, duration=2) #moves to (100, 100) in 2 secs
        #or if you just want to move the mouse, without specifying a position:
        pyautogui.move(None, 10, duration=2) #moves 10 pixels in 2 secs
    

    Simply adjust the duration parameter to control the amount of time the mouse takes to move. I've been using pyautogui for a while now, and it has proven to be quite useful. I got this information from https://pypi.org/PyAutoGUI/, where you can find the link to the full documentation.

    If pyautogui doesn't work for you, then I recommend checking out pynput, and there are plenty of stackoverflow posts about it (I'm not that experienced using pynput).