gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:56:47 24 4
gpt4 key购买 nike

在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com