1

I've been experimenting with tracking area, and having some problems, so I created this simple program as a test. I create one tracking area in the lower left corner of my view (which is the window's content view), but I receive mouseEntered and exited messages no matter where I enter or exit the view. I've also tried putting this code in the init method and awakeFromNib with the same results.

@implementation Parent //This view is the contentView of the main window

-(void)viewDidMoveToWindow{
    NSLog(@"In viwDidMoveToWindow");
    NSTrackingArea *area = [[NSTrackingArea alloc]initWithRect:NSMakeRect(0,0,50,50) options:NSTrackingInVisibleRect |NSTrackingMouseEnteredAndExited |NSTrackingActiveInActiveApp owner:self userInfo:nil];
    [self addTrackingArea:area];
}

-(void)mouseEntered:(NSEvent *)theEvent {
    NSLog(@"Entered");
}

-(void)mouseExited:(NSEvent *)theEvent {
    NSLog(@"Exited");
}

@end

Why is the tracking area not being respected?

Jasonw
  • 5,054
  • 7
  • 43
  • 48
rdelmar
  • 103,982
  • 12
  • 207
  • 218

1 Answers1

1

It has to do with the options you are using, try instead using

options:NSTrackingActiveAlways | NSTrackingMouseEnteredAndExited
Grant Wilkinson
  • 1,088
  • 1
  • 13
  • 38
  • So why does NSTrackingInVisibleRect not work as I expect? I thought the purpose of that choice was to only allow tracking in the visible portion of the rect specified in the tracking area -- it seems to be in the whole view that the tracking rectangle is added to. – rdelmar Apr 04 '12 at 04:16
  • Ok, I get it now -- I reread the docs, and it says with the NSTrackingInVisibleRect option, the value returned by rect is ignored, and the rect is automatically updated with changes in the visible rect. – rdelmar Apr 04 '12 at 04:32
  • It would seem that you could've used NSTrackingInVisibleRect in this case but according to the docs the value returned from rect is ignored. I found this answer from searching to be quite similar [link](http://stackoverflow.com/questions/4136696/nstrackingarea-works-weird-entire-view-or-nothing-no-rectangles-respected) – Grant Wilkinson Apr 04 '12 at 04:45