0

I want to change the mouse cursor to a laser pointer when someone clicks on a button using python script. How to do it on MAC?

In windows I have done like this to do the same

hnew = win32gui.LoadImage(0,  resource_path('laser.cur'), win32con.IMAGE_CURSOR, 0, 0, win32con.LR_LOADFROMFILE)
ctypes.windll.user32.SetSystemCursor(hnew, 32512)

But it is not supported on mac as it is window specific library.

1 Answer 1

0

You need access to the Cocoa frameworks which seems possible with this library (not tested). Within Cocoa the object to call is NSCursor. In Objective-c you set the cursor like this :

[NSCursor.arrowCursor set]

You can also use a pair of command to change the cursor and reset it after the action :

[NSCursor.pointingHandCursor push]
// Do an action
[NSCursor pop]

To create a custom cursor, you use a NSImage you create from a path (here I do it with a pdf image to support high resolution screen) :

NSString * pathToAnImage = "A/B/C.pdf"
NSImage * image = [NSImage multiRepresentationImageFromPDF: pathToAnImage withMaxScaleFactor:4];
NSPoint hotSpot = NSMakePoint(8, 8); //the relative position within the image interpreted as the cursor location

NSCursor * customCursor = [[NSCursor alloc] initWithImage:image hotSpot:hotSpot]]

Then you can use it as any other system cursor.

Sign up to request clarification or add additional context in comments.

1 Comment

That will allow me to replace the mouse cursor with a custom laser cursor?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.