Intercept Menus with MBS Xojo Plugins
For MBS Xojo Plugins 25.0, we add a way to better use our NSMenuMBS and NSMenuItemMBS class with Xojo MenuItems class.
The NSMenuPopupEventsMBS class allows you to intercept a menu before the system shows it. These events are available:
- didCloseMenu(menu as NSMenuMBS, theEvent as NSEventMBS, view as NSViewMBS)
- popUpMenuAtPosition(menu as NSMenuMBS, item as NSMenuItemMBS, location as NSPointMBS, view as NSViewMBS) as Boolean
- willOpenMenu(menu as NSMenuMBS, theEvent as NSEventMBS, view as NSViewMBS)
For a popup menu, the popUpMenuAtPosition method fires first. Then for any context or popup menu, you may see a willOpenMenu event just before the menu shows. Later you get a didCloseMenu event for the menu closing.

Let's make an example where you show a contextual menu in a button:
Now you have three menu items and we like to add key equivalents with alternatives. In the willOpenMenu we get the three menu items and assign the shortcuts as well as the alternate flag. The alternate flag makes macOS only show the entry matching the currently pressed down modifier keys. This way only one of them shows up for the user.
Since you get the menu items, you can also trigger some directly. For this we add a PerformAction method to NSMenuItemMBS class. So if macOS adds a standard menu item, you could run it by finding the entry and performing it.