Frank Dillon
Frank Dillon

Reputation: 11

Is there a way to change windows mouse cursors to a custom one using python?

I want to be able to change the windows mouse cursors into a custom one (with the .ani or .cur extention) globally using a python script. Is there any package or module that can help me accomplish this?

Upvotes: 1

Views: 2636

Answers (4)

MPB.RIP
MPB.RIP

Reputation: 103

Yes, it's possible to change the Windows cursor globally using Python. I couldn't find a solution that worked for me at first, but with some help from Chat GPT I developed a working method.

Proof of Concept:

Here's a Python script that demonstrates how to change the Windows cursor:

import time
import ctypes

# Set the cursor type (IDC_ARROW = 32512 for standard arrow)
cursor_type = 32512

# Load the current cursor (for later reset)
original_cursor = ctypes.windll.user32.LoadCursorW(0, cursor_type)

# Load custom cursor from file
file_path = "C:/Path/to/cursor.cur"
custom_cursor = ctypes.windll.user32.LoadCursorFromFileW(file_path)
if not custom_cursor:
    raise Exception("Failed to load cursor from file.")

# Set the arrow cursor to the custom cursor
ctypes.windll.user32.SetSystemCursor(custom_cursor, cursor_type)
print("Custom cursor set...")

# Wait 10 seconds to see the change
time.sleep(10)

# Reset the cursor to the original cursor
ctypes.windll.user32.SetSystemCursor(original_cursor, cursor_type)
print("Reset to original cursor...")

# Wait another 10 seconds to see the reset
time.sleep(10)

# Finally, reset the cursor to the Windows default settings
ctypes.windll.user32.SystemParametersInfoW(0x0057, 0, None, 0)
print("Reset to Windows default cursor...")

This script:

  1. Loads the original system cursor and a custom cursor from a file.
  2. Sets the system cursor to the custom cursor.
  3. Waits 10 seconds before returning to the original cursor.
  4. Waits another 10 seconds, then resets to the Windows default cursor.

Error Handling:

  • It's important to handle potential errors, such as the custom cursor file not being found. The script uses a simple check to make sure the cursor is loaded correctly.

Administrative Privileges:

  • Note that changing system settings such as the cursor may require administrative privileges on some systems.

Safety and Reversibility:

  • The changes made by this script are temporary and reversible. It's designed to return the cursor to its original state to ensure safe experimentation.

My GitHub Repo

Check out my GitHub repository for more details, examples, and context management implementations: PY_WIN_CURSOR.

Upvotes: -2

ori6151
ori6151

Reputation: 592

Yes (in windows), in windows: you need to change a registry key, then tell windows to load new cursor.
This code does the trick:

import os
import ctypes

path = r"HKEY_CURRENT_USER\Control Panel\Cursors"
cur_loc = r"path\to\cursor.cur"

os.system(f"""REG ADD "{path}" /v Arrow /t REG_EXPAND_SZ /d "{cur_loc}" /f""")

ctypes.windll.user32.SystemParametersInfoA(0x57)

Upvotes: 0

user2622016
user2622016

Reputation: 6413

It cannot be done globally, since it would break a security constraints. Application is not allowed modify anything over other applications or system windows.

It can be done by system C API only by processes run by root/administrator. Probably no one made a Python bindings for such functions. It is possible, but due to limited use I don't think there is any Python module doing that.

Upvotes: 1

Eros Guil
Eros Guil

Reputation: 31

you can use your own image as the mouse cursor. Use pyglet.image.load to load the image, then create an ImageMouseCursor with the image and "hot-spot" of the cursor. The hot-spot is the point of the image that corresponds to the actual pointer location on screen, for example, the point of the arrow:

image = pyglet.image.load('cursor.png')
cursor = pyglet.window.ImageMouseCursor(image, 16, 8)
window.set_mouse_cursor(cursor)

You can even render a mouse cursor directly with OpenGL. You could draw a 3-dimensional cursor, or a particle trail, for example. To do this, subclass MouseCursor and implement your own draw method. The draw method will be called with the default pyglet window projection, even if you are using another projection in the rest of your application.

Upvotes: 0

Related Questions