0

I am writing a script that intercepts a touchpad output and send to the windows after some processing. So there is no GUI involved. I want to change the cursor temporarily when certain cursor behavior occurs. I have searched the web to the best of my abilities and found very few posts that talked about using win32api.SetCursor() but this does not work at all. Most of the posts talks about changing cursor using Tkinter or wxPython. Is there any other solution to change the cursor system wide?

2 Answers 2

1

Using the code below the cursor is changed system-wide though I have to restored to the arrow cursor below quitting the program. If there are other better ways I would appreciate your response.

from ctypes import *
import win32con

SetSystemCursor = windll.user32.SetSystemCursor #reference to function
SetSystemCursor.restype = c_int #return
SetSystemCursor.argtype = [c_int, c_int] #arguments

LoadCursorFromFile = windll.user32.LoadCursorFromFileA #reference to function
LoadCursorFromFile.restype = c_int #return
LoadCursorFromFile.argtype = c_char_p #arguments

CursorPath = "../cursor/MyCross.cur"

NewCursor = LoadCursorFromFile(CursorPath)

if NewCursor is None:
    print "Error loading the cursor"
elif SetSystemCursor(NewCursor, win32con.IDC_ARROW) == 0:
    print "Error in setting the cursor"
0
import win32con
import win32api
import win32gui
import ctypes
import time
import atexit


cursor = win32gui.LoadImage(0, 32512, win32con.IMAGE_CURSOR, 
                            0, 0, win32con.LR_SHARED)
save_system_cursor = ctypes.windll.user32.CopyImage(cursor, win32con.IMAGE_CURSOR, 
                            0, 0, win32con.LR_COPYFROMRESOURCE)



cursor = win32gui.LoadImage(0, "file.cur", win32con.IMAGE_CURSOR, 
                            0, 0, win32con.LR_LOADFROMFILE);
ctypes.windll.user32.SetSystemCursor(cursor, 32512)
ctypes.windll.user32.DestroyCursor(cursor);

I hope this helps

1
  • 3
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.
    – AlexK
    Nov 29, 2022 at 22:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.