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?
It has to do with the options you are using, try instead using
options:NSTrackingActiveAlways | NSTrackingMouseEnteredAndExited