Re: Question about menuHasKeyEquivalent:forEvent:target:action:
Re: Question about menuHasKeyEquivalent:forEvent:target:action:
- Subject: Re: Question about menuHasKeyEquivalent:forEvent:target:action:
- From: Ken Thomases <email@hidden>
- Date: Mon, 07 Oct 2013 15:19:31 -0500
On Oct 7, 2013, at 3:09 PM, Steve Mills wrote:
> On Oct 7, 2013, at 14:50:31, Ken Thomases <email@hidden> wrote:
>
>> On Oct 7, 2013, at 2:33 PM, Steve Mills wrote:
>>
>>> To work around this, I am simply returning NO from menuHasKeyEquivalent and calling performActionForItemAtIndex: from it. Is that a valid workaround?
>>
>> the answer to your question […] is NO.
>
> Here, sender should be an NSMenuItem, not an NSMenu. To work around *that* problem, I'm calling performActionForItemAtIndex directly in my menuHasKeyEquivalent. The menu title in the menubar correctly flashes, so it doesn't *seem* like it's a bad thing to do.
The problem is that you can't be sure that -menuHasKeyEquivalent:… is only called when a matching menu item's action would be performed. The frameworks might have other reasons for invoking that method.
Also, if you answer NO, then presumably NSMenu keeps searching and may find another match (perhaps not even among your menu items; perhaps for a Service or whatever). Then, both actions would fire.
>> The -menuHasKeyEquivalent:… delegate method is mostly not very useful. As discussed in the WWDC 2010 Session 145 - Key Event Handling in Cocoa Applications video (around 6:55), the main use of that method is to return NO for menus that you know do _not_ have key equivalents, to optimize the search.
>
> I didn't realize I had to also watch videos in order to find documentation. The actual documentation doesn't say what it's "main reason" is, simply that you can return YES if an item handles it and supply and target and action, or NO if no items handles it. This seems like a pretty good way to work around the Apple bug. If Apple knows something different, it should be in readable/searchable documentation, not a video.
Well, sure, I agree that the written documentation should be complete. It often isn't, though, and one needs to consult headers, WWDC videos, or even the lore of your fellow developers. But, having been directed to another source of information, it doesn't seem wise to simply ignore it out of irritation.
Regards,
Ken
_______________________________________________
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