Is there any simple way to determine if the command key is being held when selecting a menu item? Thanks in advance. (newbie).
Try this:
BOOL commandKeyDown = [[NSApp currentEvent] modifierFlags] & NSCommandKeyMask;
You might also have some problems if the user tries to use the Command-
http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/ObjC_classic/Classes/NSMenuItem.html
(which is the best page on the ADC site I could find in 2 minutes). To use alternate menu items, just go into Interface Builder, put your alternate menu item below the normal one, change one of the modifier keys, and click “Treat as alternate to the previous item” (there’s instructions there too). Overall, I think this is probably a better solution than checking a specific event mask, especially the Command key. But if someone out there knows better, please correct me. –JediKnil
The original example doesn’t work, as ( modifiers & mask ) is truncated when copied into the BOOL. A working alternative is:
BOOL commandKeyDown = (([[NSApp currentEvent] modifierFlags] & NSCommandKeyMask) == NSCommandKeyMask);
If you need to get the Command key state asynchronously (e.g. when your app is in the background and is not receiving keyboard events, or during preflight before the Cocoa event loop starts) you can use the Carbon function GetCurrentKeyModifiers:
BOOL IsCommandKeyDownRightNow(void) { return 0 != (GetCurrentKeyModifiers() & cmdKey); }