Move mouse cursor to second monitor using pyautogui

10,556

Nevermind, I have found the answer from a github post.

It appears that moving the cursor to a second monitor can be accomplished using

ctypes.windll.user32.SetCursorPos(2759,769)

Sweet!

Share:
10,556
user32882
Author by

user32882

Updated on June 19, 2022

Comments

  • user32882
    user32882 about 2 years

    I started using pyautogui about an hour ago. Very nice that python supports GUI automation. I'm having one problem though. I use two screens and it appears that the wrapper is unable to move my cursor to my secondary monitor. For instance:

    >>> import pyautogui
    >>> pyautogui.moveTo(2759,769)
    >>> pyautogui.position()
     (1919, 769)
    

    As you can see the cursor is still within my first screen.

    How do I force pyautogui to move to my second screen?

    Thanks

  • Vasily Ryabov
    Vasily Ryabov over 6 years
    pywinauto (a bit different library you tagged in the question) can do the same: pywinauto.mouse.move((2759, 769)) because it uses SetCursorPos internally. But pywinauto is more text oriented vs. image/coordinates oriented PyAutoGUI.
  • Skandix
    Skandix about 6 years
    use cursor = ctypes.wintypes.POINT() and ctypes.windll.user32.GetCursorPos(ctypes.byref(cursor)) to get correct position on multiple monitors
  • bobsmith76
    bobsmith76 almost 4 years
    Do you know how to do this for a mac?