NSMenuItem Key Equivalent not working when menu is hidden
Asked Answered
B

2

7

I have an NSMenu in Interface Builder that is connected to an NSTableView's menu outlet.

I want to assign Key Equivalents to the items in this menu so the user doesn't have to right click, but they don't work unless the menu is visible (defeating the point).

Is there any way to allow the key equivalent to work when the menu is not visible?

Bing answered 30/11, 2012 at 12:27 Comment(2)
Can you put the items in the application's menubar instead?Costplus
I'd rather not, as it is more intuitive to the user for these items to be on the context menu.Bing
C
5

You can put the menu items in both, the application's menubar and the context menu. The key equivalent will then work even without the context menu being visible.

Chromic answered 13/1, 2013 at 20:51 Comment(4)
Good idea - thinking outside the box. Barring a fix, I think this workaround will work. ThanksBing
it's not the best solutions, in my case, I'm having a lot of windows, I can fire the menu item event in window A but can't in window BRiposte
This is a nice way of defining the user-interface for actions in a single place, then making sure your responder chain handles everything properly.Standifer
Unfortunately you cannot use a single instance of the NSMenu or the specific NSMenuItems - they need copying.Standifer
M
5

Looks like the macOS 10.13 SDK (High Sierra) has a new property allowsKeyEquivalentWhenHidden on NSMenuItem.

Micronesian answered 11/10, 2017 at 9:56 Comment(3)
allowsKeyEquivalentWhenHidden does not work for me. It seems to have no effect.Sletten
'hidden' does not mean that the whole menu is currently not visible on screen, 'hidden' is a named state of NSMenuItem.Standifer
I had to tick Allowed When Hidden in the top Menu Item's attribute inspector. i.e. I have an app menu item called 'Show' with 'Play' and 'Stop' items inside it. I had to tick the option in the 'Show' menu item as well as the 'Play' and 'Stop' items.Raseta

© 2022 - 2024 — McMap. All rights reserved.