How to change the size of the system cursor in a macosx cocoa app programmatically using swift or objective-c?

帅比萌擦擦* 提交于 2019-12-10 10:01:44

问题


I've tried to set it's global size using this code:

-(void)setOption {
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   NSDictionary *olddict = [defaults persistentDomainForName:@"com.apple.universalaccess"];
   NSMutableDictionary *newdict = [olddict mutableCopy];
   [newdict setObject:@4.0 forKey:@"mouseDriverCursorSize"];
   [defaults setPersistentDomain:newdict forName:@"com.apple.universalaccess"];
   [defaults synchronize];
   NSLog(@"Cursor size set to %@", newdict);
}

And I can see in the NSLog that it changed it but I don't know how to restart/reset the system cursor in order for the cursor to change to the specified size.

Does anybody know a better way to change it's size programmatically or how to restart the system cursor after the defaults change?

EDIT(about duplication): My question is unique because I can't use applescript in resolving this like the answer provided in the other topic. Also the topic has been created in 2013, and seems outdated. Maybe things have changed a little since then. Also maybe Swift would be a viable solution for resolving this. Who knows? All these arguments make it clear that this is not a duplicate question.


回答1:


CGError state = CGSShowCursor(CGSDefaultConnection) ;
    if (state != kCGErrorSuccess) NSLOG(@"error : %d",state);

maybe try it with CGSShowCursor(CGSMainConnectionID())

This might also help : https://github.com/alexzielenski/Mousecape/blob/1d534b1e076b07a01b80364be23c88c8439028bc/Mousecape/mousecloak/NSCursor_Private.h




回答2:


Warning. This code is not based on what is saved in preferences so combine it:

  float cursorScale = 2;
  cursorScale = MAX(1, MIN(cursorScale,4));
  int connectionID = CGSMainConnectionID();
  CGSSetCursorScale(connectionID, cursorScale);

to get the size

  CGSGetCursorScale(connectionID, &cursorScale);

,

   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
   NSDictionary *olddict = [defaults persistentDomainForName:@"com.apple.universalaccess"];
   NSMutableDictionary *newdict = [olddict mutableCopy];
   [newdict setObject:@4.0 forKey:@"mouseDriverCursorSize"];
   [defaults setPersistentDomain:newdict forName:@"com.apple.universalaccess"];
   [defaults synchronize];
   NSLog(@"Cursor size set to %@", newdict);

CREDITS: Alex Zielenski



来源:https://stackoverflow.com/questions/36283844/how-to-change-the-size-of-the-system-cursor-in-a-macosx-cocoa-app-programmatical

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!