Home > Mobile >  Use same keyEquivalent in NSPopupButton menu as in Main menu
Use same keyEquivalent in NSPopupButton menu as in Main menu

Time:09-18

I have an NSPopUpButton that has a few menu items that expose identical functionality to some main menu items. Those are actions most relevant to the users current context.

To signal that those items also have keyEquivalents (shortcuts), I wanted to set their keyEquivalents to the same keys as the items in the main menu.

Unfortunately that doesn't seem to work, since the conflicting items now have their keyEquivalents removed in the main menu (presumably automatically by AppKit).

Is it somehow possible to show the same keyEquivalent in the NSPopUpButton menu?

CodePudding user response:

Normally context menus are not the most logical place to show shortcut info. However, if you really want this, then the best / probably only working option is to reuse the menu in the main menu that you want to show in your NSPopUpButton, like this:

myPopUpButton.menu = (NSApp.delegate as! AppDelegate).myMenu

In this example, myMenu is an @IBOutlet to one of the NSMenu items of the application main menu.

If you want to show a slightly different menu in your popup, then you can implement menuWillOpen of the NSMenuDelegate to dynamically hide or add menu items.

  • Related