MenuKey
MenuKey Find menu and item associated with command key
#include <Menus.h> Menu Manager
long MenuKey(theKey );
char theKey ; character code from EventRecord. message
returns HiWord is menu ID or 0; LoWord is item number
Given a character code (typically from a -modified keyDown event),
MenuKey returns a code indicating which menu and item is associated with
that keystroke, if any. This also highlights the selected menu title.
theKey is a character code. It is normally the character portion of the
message field of an EventRecord when the key is down.
Returns: a 32-bit long that indicates which menu and item was selected. It is
made up of two values as follows:
High Word menu ID of selection (0 means none)
Low Word item number of selection

Notes: MenuKey looks through all enabled items in all menus in the menu list
attempting to find an item that is associated with theKey . If no match can be
found, it returns a "cancel" code (the high word of the return value is 0).
Otherwise, the return code's high word is the Menu ID and the low word is
the item ID of the item associated with the command key.
The search is case-insensitive; e.g., a theKey value of "E" or "e" will
match the first menu item associated with E. If more than one item
equates to that key, the search will find the topmost of those items in the
leftmost menu. Disabled items are not included in the search.
Note: on 256K ROMs (and Systems >= 4.1) MenuKey also searches
through the hierarchical portion of the menu list (i.e., through menus
inserted with beforeID = -1). A match may be found in a submenu even if
that submenu is not currently attached to a menu. The hierarchical
portion of the menu list is searched last.
When a match is found, the parent (or ancestor) menu title in the
menubar is hilighted and the menu ID returned is that of the child menu
(not the hilighted menu). The ID of the hilighted menu may be found in the
global variable TheMenu.
A command key is associated with a menu item when its item text (as used
in AppendMenu or InsMenuItem) contains the /" meta character, e.g.:
InsMenuItem( myEditMenu, "Undo/U", 0 );
or after a call to SetItemCmd (256K ROMs).
A typical application first checks the EventRecord.modifiers field for the
bit, then extracts the character portion of the message field using the
defined constant charCodeMask. Then it calls MenuKey to see if the key
belongs to a menu and finally invokes the appropriate routine to handle the
event-normally the same function that handles the event when selected via
MenuSelect. After processing, remember to unhighlight the menu title
via HiliteMenu(0).
GetNextEvent( everyEvent, &theEvent );
switch ( myEvent.what ) {
case keyDown:
theKey = theEvent. message & charCodeMask;
if (( theEvent.modifiers & cmdKey) != 0) {
menuResult = MenuKey( theKey );
if ( HiWord( menuResult) != 0 ) /* valid key ? */
doCommand( menuResult ); /* yes, do it */
}
else
doKeystroke(); /* process normal keystroke */
break;
case mouseDown:
/* ... handle mouse click ... */
}
Keystrokes that include the -key may also be discovered as repeat keys
(EventRecord.what = autoKey ). In that case, it is up to you to decide
whether it is appropriate to take action or ignore the repeat.