NSMenuItem keyEquivalent does not show up

Hello,


I am making an NSMenuItem like this:



if let mainMenu = NSApplication.shared.mainMenu {
       if let fileMenuItem = mainMenu.item(withTitle:"File") {
            if let fileMenu = fileMenuItem.submenu {  
                    let export = fileMenu.insertItem(withTitle:"Export", action:#selector(Filer.sharedInstance.Export), keyEquivalent:"e", at:5)
                    export.target = Filer.sharedInstance
                    export.isEnabled = true
                    export.keyEquivalentModifierMask = .command
                }
            }
        }

I want it to show the Export command in the menu with "⌘E", but it dossn't. HOWEVER, if I change the "e" to "E" above, then it does show up -- but it wants me to use the shift key as well. That is, the menu looks like this: "Export ⇧⌘E".

Is there some reason it won't let me use a lower-case E? I know that the menus always show upper case letters. But I don't want to have to press shift. Just like, for example, the menu for Print shows "⌘P" but you can actually just hit "command-p" to print...


Is command-e used for somethng else and that's why it's stopping me?


EDIT: Looks like command-E is a Finder shortcut for Eject. Can I override that in my app so that I can use it for export in my menu?


Thanks

Replies

The following code…


        if let mainMenu = NSApplication.shared.mainMenu {
            if let fileMenuItem = mainMenu.item(withTitle:"File") {
                if let fileMenu = fileMenuItem.submenu {
                    let export = fileMenu.insertItem(withTitle:"Export", action:nil, keyEquivalent:"e", at:5)
                    export.isEnabled = true
                    export.keyEquivalentModifierMask = .command
                    print("keyEquivalent: \"\(export.keyEquivalent)\"")
                }
            }
        }


…prints…


keyEquivalent: ""


However, if a separate menu item with the shortcut Command+E is added to the menu in Interface Builder, the same code prints…


keyEquivalent: "e"


Strange.

Does your app have a Find submenu of the Edit menu? Command-E is the system-wide conventional key equivalent for the Use Selection for Find menu item. The Finder's Eject menu item would not interfere with your app's menu item, but another menu item within your own app's menus would.

I don't believe it has a Find subview in the Edit menu. I don't see one. There may have been one originally that I deleted from the storyboard. I don't see anything else in the menus that shows a shortcut for 'e'.

Does any menu in the menu bar highlight briefly when you press Command-E in your app? Does any Service in the Services submenu of the application menu use Command-E as its key equivalent?

No -- but actually it works now. Yesterday I had changed it to use comand-shift-E because the other wasn't working. But it was probably some time after that that I deleted the extraneous menus, and I bet that the Find menu was one of those.

Thanks!

"e" is already assigned for the main menu Edit > Find > Use Selection for Find ⌘E. You can't assign an already existing keyEquivalent letter. You need to choose another letter or change the already existent one.