2

I am trying to get a custom cursor for my OSX application. I've read about doing this without success for the last few hours. So i've moved to the simplest aspect, simply setting the current cursor as the crosshair cursor, in a project that has absolutely nothing else.

Here is the most basic setup which does not change the cursor for me. Using XCode, I made a new Cocoa project. The only piece of code I wrote is one single line in the following function.

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
        [[NSCursor crosshairCursor] set];
}

This does not change the cursor, which probably means I am missing something extremely basic, but cannot figure out for the life of me.

I tried many variations, using the NSCursor -push method to change the current cursor, and it does not work. I also tried putting an NSButton and then doing as a different answer suggests here (where button is of the type NSButton *):

[button addCursorRect:[button bounds] cursor:[NSCursor crosshairCursor]];

I've written a couple apps for the iPhone before, but never for the Mac, but it seems pretty similar. Am I missing something hopelessly stupid? I also tried playing with the addCursorRect:cursor: method, but I'm not sure I did it right. It wasn't clear what function is supposed to call it, and from where.

Once I figure out how to show ANY different cursor, then I want to move on to making my own images for the cursor.

Thanks!

Community
  • 1
  • 1
AJJ
  • 105
  • 1
  • 2
  • 5

1 Answers1

2

I'm not sure why, but setting the cursor in applicationDidFinishLaunching doesn't seem to work. (I just tried it.) But it will work if you do it later, like as the result of clicking a button or maybe use a timer if you want it to happen at launch.

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timer:) userInfo:nil repeats:NO];
}

-(void)timer:(id)foo
{
    [[NSCursor pointingHandCursor] set];
}
Ken Aspeslagh
  • 11,484
  • 2
  • 36
  • 42
  • Thanks! That does work for me! This reminds me of very similar issues I had when developing for the iPhone. For anyone who is having problems even with the iphone with some commands not working such as having the camera fire, etc., make sure you do it in a later function or after a delay as indicated above. I've seen commands not give the desired results when done in applicationDidFinishLaunching. There may/may not be a reason for this, but it does suffice as a workaround for now. – AJJ Aug 05 '11 at 18:45
  • 2
    Good to hear. Maybe Apple should call the callback applicationAlmostButNotCompletelyFinishedLaunching ;) – Ken Aspeslagh Aug 05 '11 at 21:40