Re: Question about menuHasKeyEquivalent:forEvent:target:action:
Re: Question about menuHasKeyEquivalent:forEvent:target:action:
- Subject: Re: Question about menuHasKeyEquivalent:forEvent:target:action:
- From: Steve Mills <email@hidden>
- Date: Mon, 07 Oct 2013 15:09:41 -0500
On Oct 7, 2013, at 14:50:31, Ken Thomases <email@hidden> wrote:
> On Oct 7, 2013, at 2:33 PM, Steve Mills wrote:
>
>> Why is it that if I use the menu delegate method menuHasKeyEquivalent:forEvent:target:action: to search for a key equiv in one of our submenus, the action method gets called with the menu as the sender instead of the menu item? I see now way to say "use this menu item as the sender when you send the action to the target".
>>
>> To work around this, I am simply returning NO from menuHasKeyEquivalent and calling performActionForItemAtIndex: from it. Is that a valid workaround?
>
> What exactly are you trying to do? Are you perhaps just looking for -[NSMenu performKeyEquivalent:]?
I'm trying to work around an Apple bug described in my previous question to this list, "performKeyEquivalent is finding the wrong menu item" when item 1 uses unmodified-1 and item 2 uses control-1.
> The behavior of your -menuHasKeyEquivalent:... method is up to you. Are you asking why your method does what it does? We can't know. In any case, calling that should not invoke the action on any target. So: a) I don't understand your first paragraph, and b) the answer to your question in the second paragraph is NO.
If menuHasKeyEquivalent returns YES, then the method that gets called to handle the menu item (the action) is given the menu as the sender (the same menu that was asked menuHasKeyEquivalent, not even the submenu that directly owns the menu item that was found). Normally, the menu *item* is sent as the sender to the action.
-(IBAction) doSomething:(id)sender
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 -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.
--
Steve Mills
office: 952-818-3871
home: 952-401-6255
cell: 612-803-6157
_______________________________________________
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