title |
---|
Shortcut of NSMenuItem and NSButton |
Table of Content
Shortcut key, or hot key, is called Key Equivalent
in Mac development. To use it, just press corresponding key combination, it will trigger predefined application function.
Among NSControl’s subclass, NSMenuItem and NSButton supports key equivalent, which can be implemented in programmatic way or by changing xib object property.
Normally, key equivalent is composite of two parts: some modifier keys and one basic key.
There are 4 possible modifier keys:
Symbol | Name |
---|---|
⌘ | Command |
⌃ | Control |
⇧ | Shift |
⌥ | Option, alias Alternate |
Normal key, includes alphabet, number, punctuation, symbol key, etc.
Key Equivalent may have no modifier key. This is very common in high-efficiency professional application, for example, in Photoshop, moving layer’s shortcut key is V
.
For symbol key, it refers to key like ⎋
(escape), ↑
(up arrow). On Mac laptop, ⌦
(delete), ⇞
(page up), ⇟
(page down), ↖
(home), ↘
(end) does not have dedicated physical keys. But there are substitutes to accomplish same function:
Key | Substitute |
---|---|
Delete | Fn + ⌫ |
Page Up | Fn + ↑ |
Page Down | Fn + ↓ |
Home | Fn + ← |
End | Fn + → |
In Attributes panel of NSMenuItem/NSButton, Key Equivalentinput field, is used to set shortcut. For example, a character, E, will be displayed capitalized.
If shortcut includes ⇧
key, the input field will show an Alternates button, to let you choose displaying style between, like ⌘+
and ⇧⌘=
.
Following code sets menuItem’s shortcut as E
.
[menuItem setKeyEquivalentModifierMask:!NSEventModifierFlagCommand];
[menuItem setKeyEquivalent:@"e"];
Following code sets menuItem’s shortcut as ⌘E
.
[menuItem setKeyEquivalent:@"e"];
Following code sets menuItem’s shortcut as ⇧⌘E
.
[menuItem setKeyEquivalent:@"E"];
First of all, you need to know corresponding Unicode value.
Symbol | Name | Unicode |
---|---|---|
⌫ | Backspace | 0x0008 |
⇥ | Tab | 0x0009 |
↩ | Return | 0x000d |
⎋ | Escape | 0x001b |
← | Left | 0x001c |
→ | Right | 0x001d |
↑ | Up | 0x001e |
↓ | Down | 0x001f |
␣ | Space | 0x0020 |
⌦ | Delete | 0x007f |
↖ | Home | 0x2196 |
↘ | End | 0x2198 |
⇞ | Page Up | 0x21de |
⇟ | Page Down | 0x21df |
Note: NSBackspaceCharacter
, NSTabCharacter
, NSCarriageReturnCharacter
are defined in NSText.h, others are not. All of them (except 4 arrow keys) can be found here. Arrow keys do not use 0x2190, 0x2191, 0x2192, 0x2193, I don’t know the reason.
Following code sets menuItem’s shortcut as ⌘↩
.
NSString *s = [NSString stringWithFormat:@"%c", NSCarriageReturnCharacter];
[menuItem setKeyEquivalent:s];
Following code sets menuItem’s shortcut as ⌘↑
.
NSString *s = [NSString stringWithFormat:@"%C", 0x001e];
[menuItem setKeyEquivalent:s];
Attention: the format specifiers are different, the latter MUST use %C
.
One more thing, the shortcuts displayed by menu items are different than the symbols on xib canvas and those printed on some keyboards.
In xib | Running application |
---|---|
![]() |
![]() |
Some keyboard models:
Apple - Magic Keyboard with Numeric Keypad
Belkin - YourType Bluetooth Wireless Keypad
Option key has an alias, Alternate key, it is called so when holding it, the menu item will appear in a different way.
In Apple’s Human Interface Guidelines, this menu item is called Dynamic Menu Items, and invisible by default.
For instance, click MacOS desktop’s left top
menu item, then hold option key, you will see, “About This Mac” changes to “System Information…” and its triggered action changes too.
In xib, to implement this:
- Add 2 NSMenuItem, they MUST be adjacent, no other menu item between them.
- Set valid shortcut for them, and the second’s shortcut MUSTbe the first’s shortcut, plus
⌥
key. - Check Alternate property for the second NSMenuItem.
Now, run the app and it will works as above
menu item example.
Besides, in step 2, if you switch shortcuts of these two menu items, the default visible will be the second one instead, while they’re still alternate.
- Do NOT add shortcut for contextual menu. Link
- Do NOT conflict with system shortcuts or other popular shortcuts, like
⇧⌘Q
(log out account),⌘C
(copy), etc. - Only add shortcut for frequently used action, to relieve user’s learning and remembering burden. For example, about, is a rarely-used action, it does not need a shortcut.
⌃⇧1
and⇧1
can not exist together, or the former triggers the latter’s action.⌃⌥⇧1
and⇧1
can not exist together, or the former triggers the latter’s action.⌃⌥⇧1
and⌃⇧1
can not exist together, or the former triggers the latter’s action.⌃⇧A
, its log info shows incorrectly for ⌃A, both in code and xib. What’s more, In code, ifkeyEquivalent
is a capitalized alphabet andkeyEquivalentModifierMask
does not includeNSEventModifierFlagShift
, system will add⇧
automatically in the shortcut UI.- In xib, set a menu item or button’s Key Equivalent with
⇧
,⌘
,=
, and choose alternates as⌘+
, then⌘=
and⇧⌘=
can both trigger its action. - When setting
keyEquivalentModifierMask
for NSButton, it can not includeNSEventModifierFlagControl
, or shortcut will not work.
- Human Interface Guidelines
- Application Menu and Pop-up List Programming Topics
- https://stackoverflow.com/a/6230866/353927
- Unicode character table
Here is demo project.
Repost from https://cool8jay.github.io/shortcut-nemenuitem-nsbutton/