Cocoa 中的选项 (⌥) + 上下文菜单?

Option (⌥) + Context Menu in Cocoa?

在 Cocoa 应用程序中,有没有一种方法可以让用户在按住 Option (⌥) 键时轻松地在上下文菜单中显示其他或完全不同的选项?

这是在全局菜单栏的音量上下文菜单中使用的:

在Finder中也有使用,但我无法截图,因为截图的快捷方式取消了Option(⌥)上下文菜单。

看看 NSView 的 menuForEvent: method and NSMenuItem's alternate 属性。 setAlternate: 的讨论不见了,这里是:

Discussion

If the receiver has the same key equivalent as the previous item, but has different key equivalent modifiers, the items are folded into a single visible item and the appropriate item shows while tracking the menu, depending on what modifier key (if any) is pressed. The menu items may also have no key equivalent as long as the key equivalent modifiers are different.

Consider the following example: menuItem1 and menuItem2 are menu items in the same menu, with menuItem1 above menuItem2:

[menuItem1 setTitle:@"One"];
[menuItem1 setKeyEquivalent:@"t"];

[menuItem2 setTitle:@"Two"];
[menuItem2 setKeyEquivalent:@"T"];
[menuItem2 setAlternate:YES];

When the menu is displayed, it shows only menuItem1 (with title “One”) instead of two menu items. If the user presses the Shift key while the menu is displayed, menuItem2 (with title “Two”) replaces “One”.

If there are two or more items with no key equivalent but different modifiers, then the only way to get access to the alternate items is with the mouse. In the following example,“Two” is shown only if the user presses the Alternate key.

 [menuItem1 setKeyEquivalent:@""];
 [menuItem1 setTitle:@"One"];

 [menuItem2 setKeyEquivalent:@""];
 [menuItem2 setKeyEquivalentModifierMask:NSAlternateKeyMask];
 [menuItem2 setTitle:@"Two"];

If you mark items as alternates but their key equivalents don’t match, they might be displayed as separate items. Marking the first item as an alternate has no effect.