Custom NSView in NSMenuItem not receiving mouse events
Custom NSView in NSMenuItem not receiving mouse events
- Subject: Custom NSView in NSMenuItem not receiving mouse events
- From: Dennis Hartigan-O'Connor <email@hidden>
- Date: Wed, 09 Sep 2009 04:40:03 -0700
I have an NSMenu popping out of an NSStatusItem using
popUpStatusItemMenu. These NSMenuItems show a bunch of different links,
and each one is connected with setAction: to the openLink: method of a
target. This arrangement has been working fine for a long time. The user
chooses a link from the menu and the openLink: method then deals with it.
Unfortunately, I recently decided to experiment with using NSMenuItem's
setView: method to provide a nicer/slicker interface. Basically, I just
stopped setting the title, created the NSMenuItem, and then used
setView: to display a custom view. This works perfectly, the menu items
look great and my custom view is displayed.
However, when the user chooses a menu item and releases the mouse, the
action no longer works (i.e., openLink: isn't called). If I just simply
comment out the setView: call, then the actions work again (of course,
the menu items are blank, but the action is executed properly). My first
question, then, is: why does setting a view break the NSMenuItem's action?
No problem, I thought, I'll fix it by detecting the mouseUp event in my
custom view and calling my action method from there. I added this method
to my custom view:
- (void)mouseUp:(NSEvent *)theEvent {
NSLog(@"in mouseUp");
}
No dice! This method is never called.
I can set tracking rects and receive mouseEntered: events, though. I put
a few tests in my mouseEntered routine, as follows:
if ([[self window] ignoresMouseEvents]) { NSLog(@"ignoring mouse
events"); }
else { NSLog(@"not ignoring mouse events"); }
if ([[self window] canBecomeKeyWindow]) {
dNSLog((@"canBecomeKeyWindow")); }
else { NSLog(@"not canBecomeKeyWindow"); }
if ([[self window] isKeyWindow]) { dNSLog((@"isKeyWindow")); }
else { NSLog(@"not isKeyWindow"); }
And got the following responses:
not ignoring mouse events
canBecomeKeyWindow
not isKeyWindow
Is this the problem? "not isKeyWindow"? Presumably this isn't good
because Apple's docs say "If the user clicks a view that isn’t in the
key window, by default the window is brought forward and made key, but
the mouse event is not dispatched." But there must be a way do detect
these events. HOW?
Adding:
[[self window] makeKeyWindow];
has no effect, despite the fact that canBecomeKeyWindow is YES.
I guess I am going to try to use nextEventMatchingMask next, which seems
like a unnecessarily complex hack.
Dennis
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden