Getting cursor position in Python

后端 未结 11 1153
旧巷少年郎
旧巷少年郎 2020-12-02 22:29

Is it possible to get the overall cursor position in Windows using the standard Python libraries?

11条回答
  •  借酒劲吻你
    2020-12-02 23:05

    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.

提交回复
热议问题