Neil Albarran
Neil Albarran

Reputation: 95

Detect if the mouse cursor is hidden or visible by any other application

I want to detect if the mouse is currently hidden, something that is often done by 3D applications on Windows. This appears to be trickier than it sounds as I can't find any way to do this.

Preferably I want to do this using Python but if that's not possible I could resort to C. Thanks!

Upvotes: 4

Views: 1866

Answers (2)

Adam Rosenfield
Adam Rosenfield

Reputation: 400174

You'll need to call the GetCursorInfo function. This can be done straightforwardly using the pywin32 library. Or, if you'd rather not install external Python libraries, you can use the ctypes module to directly access the function from User32.dll.

Example:

import ctypes

# Argument structures
class POINT(ctypes.Structure):
    _fields_ = [('x', ctypes.c_int),
                ('y', ctypes.c_int)]

class CURSORINFO(ctypes.Structure):
    _fields_ = [('cbSize', ctypes.c_uint),
                ('flags', ctypes.c_uint),
                ('hCursor', ctypes.c_void_p),
                ('ptScreenPos', POINT)]

# Load function from user32.dll and set argument types
GetCursorInfo = ctypes.windll.user32.GetCursorInfo
GetCursorInfo.argtypes = [ctypes.POINTER(CURSORINFO)]

# Initialize the output structure
info = CURSORINFO()
info.cbSize = ctypes.sizeof(info)

# Call it
if GetCursorInfo(ctypes.byref(info)):
    if info.flags & 0x00000001:
        pass  # The cursor is showing
else:
    pass  # Error occurred (invalid structure size?)

Upvotes: 3

xxbbcc
xxbbcc

Reputation: 17327

The GetCursorInfo function returns a CURSORINFO structure that has a flags field that contains the global cursor state. Would this do what you need? I'm not familiar with Python so I don't know if you can call this function from Python.

Upvotes: 1

Related Questions