MenuKey
MenuKey
Find menu and item associated with command key 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
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
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 switch ( myEvent.what ) {
case keyDown:
theKey = theEvent. message & charCodeMask;
if (( theEvent.modifiers & cmdKey) != 0) {
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.