Why does my NSWindow only receive mouseOver events the first time?
I have an application where a borderless window is shown and hidden using orderOut and orderFront. When this is visible, I want it to become the key window when the mouse moves over it. So far I've done this:
- In awakeFromNib, I have installed my first answer for myself.
- The window constructor I have set accepts mouse events at YES.
- In the mouseMoved method, I am using makeKeyAndOrderToFront.
My problem is that this only works the first time the mouse is moved over the window. After that, it doesn't receive any mouseOver events. I tried to check firstResponder but as far as I can tell it never changes from the window.
Any ideas what I can do to get this to work?
a source to share
You need to add a tracking scope if you want to receive events mouseMoved
(I assume you mean how Cocoa has no such thing as an event mouseOver
).
I wrote a little app called Shroud that does something similar - it hides a borderless window that closes the menu bar when you move the mouse over it. the code is simple enough, it can be useful as an example.
a source to share
Here is an example written using @NicholasRiley's answer :
NSTrackingArea *area = [[NSTrackingArea alloc] initWithRect:[self.view frame] options:NSTrackingMouseEnteredAndExited | NSTrackingInVisibleRect | NSTrackingActiveAlways owner:self userInfo:nil];
[self.view addTrackingArea:area];
-(void)mouseEntered:(NSEvent *)theEvent {
NSLog(@"mouseEntered");
}
-(void)mouseExited:(NSEvent *)theEvent {
NSLog(@"mouseExited");
}
a source to share