Re: Menus to distinguish Numpad vs normal digit keys
Re: Menus to distinguish Numpad vs normal digit keys
- Subject: Re: Menus to distinguish Numpad vs normal digit keys
- From: Peter Ammon <email@hidden>
- Date: Mon, 27 Sep 2010 12:58:06 -0700
On Sep 27, 2010, at 11:35 AM, Russ wrote:
> I have different menu keys set up in my app for control-4 and
> control-number-pad-4, but it seems that Cocoa is not distinguishing between the
> two. (There are several key equivalents in the number-pad area that are used for
> variants on directional motions, ie left, right, up, down, etc, whereas the
> usual digit accelerators are used for more usual tasks, so I need to keep the
> two separate).
>
>
> The NSMenu setKeyEquivalent routine takes Unicode key codes, right? I'd like to
> be able to instead specify the key using an Apple key code, ie AK_KP_0..9, which
> has separate codes for each flavor.
>
>
> Any suggestions on what/how to override to get the correct functionality?
>
> Thanks
Do you mean that you want to have menu items handle control-4 for the top row, but not for the numpad? If so, the simplest approach is to override either -[NSApplication performKeyEquivalent:] or -[NSMenu performKeyEquivalent:] on the main menu. Within your override, inspect the event's virtual key code (-keyCode), and return NO for the numpad.
If you mean that you want to have one menu item handle control-4 for the top row and a different menu item for the numpad, you'll have to take a more complicated route:
- Set the key equivalent like you currently do
- Implement the delegate method -menuHasKeyEquivalent:forEvent:target:action:
- Within that method, check the virtual key code of the NSEvent (that is, the -keyCode method)
That should allow you to distinguish between the two events.
A secondary issue that you'll run into is that NSMenu will prevent both items from sharing what it thinks is the same key equivalent. You can work around this by giving each item the same action.
Hope that helps,
-Peter_______________________________________________
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