I created a tracking area in NSView that should only track mouse moved events when the window is the key window. However, I noticed that sometimes, when the window is overlapped by another window which is currently key, the background window which is no longer the key window, still receives mouseMoved: events.
Here is my code in NSView subclass:
if (_trackingArea != nil) {
[self removeTrackingArea:_trackingArea];
}
_trackingArea = [[NSTrackingArea alloc]
initWithRect:trackingFrame
options:(NSTrackingAreaOptions)(NSTrackingMouseMoved | NSTrackingActiveInKeyWindow)
owner:self
userInfo:nil];
[self addTrackingArea:_trackingArea];
I added NSLog(@"is window key %d", [[self window] isKeyWindow]) in -mouseMoved: and it clearly shows that the background window is NOT key, though it still receives the mouse moved events.
This artifact disappears after I click the background window to make it key and then click the foreground window back again to make it key. Then the background window stops receiving mouse moved events.
Is this a bug of NSTrackingArea, is there any way to work around it?
UPDATE: I noticed that this bug only appears when the background window gets programmatically resized, while the foreground window has keyboard focus.
NSTrackingMouseMoved | NSTrackingActiveInKeyWindow. The code refers to a NSView subclass placed in the background window. - user1548418