问题描述
当有人使用python脚本单击按钮时,我想将鼠标光标更改为激光指示器。如何在MAC上做到这一点?
在Windows中,我这样做是一样的
hnew = win32gui.LoadImage(0,resource_path('laser.cur'),win32con.IMAGE_CURSOR,win32con.LR_LOADFROMFILE)
ctypes.windll.user32.SetSystemCursor(hnew,32512)
但是由于它是特定于窗口的库,因此在Mac上不受支持。
解决方法
您可以使用os.system()
在python上执行bash命令/脚本,然后执行如下所示的鼠标光标修改命令。
What terminal command can I use to terminate the cursor/mouse process on a MacOSX?
,您需要使用此library(未经测试)访问Cocoa框架。在可可中,要调用的对象是NSCursor。在Objective-c中,您可以这样设置光标:
[NSCursor.arrowCursor set]
您还可以使用一对命令来更改光标并在操作后将其重置:
[NSCursor.pointingHandCursor push]
// Do an action
[NSCursor pop]
要创建自定义光标,请使用从路径创建的NSImage(此处我使用pdf图像来支持高分辨率屏幕):
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]]
然后您可以将其用作任何其他系统光标。