DanieL
DanieL

Reputation: 31

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 it is visible, I want the it to become the key window when the mouse moves over it. So far I've done this:

My problem is, that this only works the first time I move the mouse over the window. After that, it doesn't receive any mouseOver events. I've tried checking the firstResponder but as far as I can tell it never changes from the window.

Any ideas what I can do to get this working?

Upvotes: 3

Views: 1914

Answers (2)

Daniel Storm
Daniel Storm

Reputation: 18898

Here's an example written with the help of @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");
}

Upvotes: 3

Nicholas Riley
Nicholas Riley

Reputation: 44321

You need to add a tracking area if you want to receive mouseMoved events (I assume that's what you mean as Cocoa has no such thing as a mouseOver event).

I wrote a little app called Shroud which does something similar — it hides a borderless window which covers the menu bar when you move the mouse over it. The code is simple enough it might be useful as an example.

Upvotes: 4

Related Questions