Is it possible to get the overall cursor position in Windows using the standard Python libraries?
win32gui.GetCursorPos(point)
This retrieves the cursor's position, in screen coordinates - point = (x,y)
flags, hcursor, (x,y) = win32gui.GetCursorInfo()
Retrieves information about the global cursor.
Links:
I am assuming that you would be using python win32 API bindings or pywin32.
Using the standard ctypes library, this should yield the current on screen mouse coordinates without any third party modules:
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)
I should mention that this code was taken from an example found here So credit goes to Nullege.com for this solution.
I found a way to do it that doesn't depend on non-standard libraries!
Found this in Tkinter
self.winfo_pointerxy()
If you're doing automation and want to get coordinates of where to click, simplest and shortest approach would be:
import pyautogui
while True:
print(pyautogui.position())
This will track your mouse position and would keep on printing coordinates.
You will not find such function in standard Python libraries, while this function is Windows specific. However if you use ActiveState Python, or just install win32api
module to standard Python Windows installation you can use:
x, y = win32api.GetCursorPos()
Using pyautogui
To install
pip install pyautogui
and to find the location of the mouse pointer
import pyautogui
print(pyautogui.position())
This will give the pixel location to which mouse pointer is at.