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

标签 macos cocoa contextmenu

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

这是在全局菜单栏的音量上下文菜单中使用的:
Here is an image of the Volume context menu in the global Menu bar
Here is an image of the Volume context menu if I hold Option (⌥) before toggling it

它也在 Finder 中使用,但我无法截取屏幕截图,因为截取屏幕截图的快捷方式会取消选项 (⌥) 上下文菜单。

最佳答案

看看 NSView 的 menuForEvent:方法和 NSMenuItem 的 alternate属性(property)。 缺少 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.

关于macos - Cocoa 中的选项 (⌥) + 上下文菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33764644/

相关文章:

python - Objective-C 数组中的连续数字,如 Python 中的 range()

macos - OS X 上的输入管理器

JavaScript 数组无法正确填充

macos - 让 pygame 在 Macos 上显示除空白屏幕以外的任何内容的问题

bash - which ctags 显示/usr/local/bin/ctags 但是当我运行ctags 时它运行/usr/bin/ctags。这怎么可能?

objective-c - 我将如何拦截 Cocoa 应用程序中的 HTTP 流量?

wpf - ContextMenu.PlacementTarget 在 WPF 中什么都不是

wpf - 如何使用 MultiBinding 在上下文菜单中隐藏分隔符?

android - “adb”未被识别为命令

macos - Vim 和 Mac : How to copy to clipboard without pbcopy