选项(⌥)+可可中的上下文菜单? [英] Option (⌥) + Context Menu in Cocoa?

查看:98
本文介绍了选项(⌥)+可可中的上下文菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In a Cocoa application, is there a way to easily allow users to display additional or completely different options in a context menu when they hold down the Option (⌥) key?

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

Here's it being used in the Volume context menu in the global menu bar:

它也在Finder中使用,但是我无法拍摄屏幕快照,因为拍摄屏幕快照的快捷方式取消了Option(⌥)上下文菜单.

It's also used in Finder, but I can't take a screenshot because the shortcut to take a screenshot cancels out the Option (⌥) context menu.

推荐答案

看看NSView的

Take a look at NSView's menuForEvent: method and NSMenuItem's alternate property. The discussion of setAlternate: is missing, here it is:

讨论

如果接收器具有与上一个项目相同的等效键,但是具有不同的等效键修饰符,则这些条目将折叠为单个可见条目,并且在跟踪菜单时会显示适当的条目,具体取决于哪个修饰键(如果有) ).菜单项也可能没有按键等效项,只要按键等效项修饰符不同即可.

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.

请考虑以下示例:menuItem1和menuItem2是同一菜单中的菜单项,而menuItem1在menuItem2上方:

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];

显示菜单时,它仅显示menuItem1(标题为一个"),而不显示两个菜单项.如果用户在显示菜单时按下Shift键,则menuItem2(标题为"Two")将替换为"One".

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".

如果有两个或两个以上没有等效键但修饰符不同的项目,则唯一的方法就是使用鼠标.在下面的示例中,仅当用户按下Alternate键时,才会显示"Two".

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.

这篇关于选项(⌥)+可可中的上下文菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆