Question

I am working on a Mac application with Xcode (using cocos2d), trying to configure the cursor, and the "set" method does not seem to have any effect in some cases...

I already had a hard time to set my cursor at the launch of the application (NSTimer to set it after the application was really launched), and now I just want it to display another image when the user clicks; I use NSNotification for that, my cursor class receives the notification, then it is supposed to set the new image, then... nothing.

Here is some code which may help:

-(void) click  
{  
    CCLOG(@"Click");  
    NSString *path = [[NSBundle mainBundle] pathForResource:@"point_pressed" ofType:@"png"];  
    NSImage *cursorImage = [[[NSImage alloc] initByReferencingFile:path] autorelease];  
    NSCursor *cursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:[[NSCursor currentCursor] hotSpot]];  
    [cursor set];  
}  

In the initialization:

    [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(updateCursor:) userInfo:nil repeats:NO];  

And the method:

-(void) updateCursor:(id)sender  
{  
    CCLOG(@"Update cursor");  
    [[self.cursorsDict objectForKey:@"point"] set];  
}  

The "updateCursor" method is also called when the application becomes active, and then it works fine, the correct cursor is displayed.
I have tried many things, pop and push methods, "setOnMouseEnter" (although I don't use rect yet), but no result...
Has anyone a clue about this?

Edit:

Stranger, I wrote the appWake method:

-(void) appWake
{
    int i = rand()%3;
    if(i==0)
        [[self.cursorsDict objectForKey:@"point"] set];
    else if(i==1)
        [[self.cursorsDict objectForKey:@"point_pressed"] set];
    else if(i==2)
        [[self.cursorsDict objectForKey:@"open"] set];
    self.state = ECursorState_Point;
    [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(appWake) userInfo:nil repeats:NO];
}

Which is called by a notification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWake) name:EVENT_APPLICATION_DID_BECOME_ACTIVE object:nil];

Set in the appDelegate by:

-(void) applicationDidBecomeActive:(NSNotification *)notification
{
    [[NSNotificationCenter defaultCenter] postNotificationName:EVENT_APPLICATION_DID_BECOME_ACTIVE object:nil];
}

And when it's called by this notification, it works fine, the cursor changes randomly; but if I remove the notification in applicationDidBecomeActive and call it somewhere else in my code, then it does not do anything (although I checked that it is called)...

Was it helpful?

Solution

I know it has been awhile. But I had a similar problem.

For some reason [cursor set] doesn't work when being called from the application events.

Here is how I did it:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    [self performSelector:@selector(doChangeCursor) withObject:nil afterDelay:1];
}

- (void) doChangeCursor
{
    NSString *file = [[NSBundle mainBundle] pathForResource:@"statusBarImage" ofType:@"tiff"];
    NSImage *image = [[NSImage alloc] initWithContentsOfFile:file];
    NSCursor *cursor = [[NSCursor alloc] initWithImage:image hotSpot:NSMakePoint(0, 0)];
    [cursor set];    
}

I hope this is able to help someone.

OTHER TIPS

Working solution for me to change the cursor from system event was to wrap the cursor set inside async like so:

DispatchQueue.main.async {
    self.customCursor.set()
}

(Swift 3)

Well, I could not find any solution to this problem. I had to use a workaround: handle a sprite cursor set at the position of the cursor...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top