1

I tried this How to get the text cursor position in Windows?, but it still not work, when mouse leaves gui's boundingbox, it does not update mouse position anymore What I want to do is to let mouse move out my python app's area, and let it move on other part of the screen, and get the position of it relative to the screen

from ctypes import windll, Structure, c_long, byref


class POINT(Structure):
    _fields_ = [("x", c_long), ("y", c_long)]



def queryMousePosition():
    pt = POINT()
    windll.user32.GetCursorPos(byref(pt))
    return { "x": pt.x, "y": pt.y}


pos = queryMousePosition()
print(pos)
martineau
  • 119,623
  • 25
  • 170
  • 301
pg daszzz
  • 49
  • 5
  • I get it by `flags, hcursor, (x, y) = win32gui.GetCursorInfo() # Get cursor position` in WIN10. – Jason Yang Sep 15 '21 at 16:26
  • I think an easier method of retrieving mouse position is using the `pyautogui` library (`pip install pyautogui`) and then using the [`.position()`](https://pyautogui.readthedocs.io/en/latest/quickstart.html?highlight=position#general-functions) function then you also probably need an `.after` loop to keep track of the position all the time if that is what you need – Matiiss Sep 15 '21 at 18:27
  • 1
    You can use tkinter `.winfo_pointerxy()` to get the mouse position periodically using tkinter `.after()`. `.winfo_pointerxy()` works even the mouse cursor is out of the tkinter window. – acw1668 Sep 16 '21 at 01:36

1 Answers1

0

The best and easiest method in my opinion is using the PyAutoGui library. You can install it using the command pip install pyautogui or python -m pip install pyautogui and using the pyautogui.position() method inside the library

TheEngineerGuy
  • 208
  • 2
  • 8