Search code examples
swiftcocoa

How can I define a NSMenuItem without having to define a keyEquivalent for it?


Currently I am using this code for defining my NSMenuItem:

let showWindow1 = NSMenuItem(title: "Show Window 1", action: #selector(showWindow1), keyEquivalent: "1")

This code works but as you can see it needs a keyEquivalent, in my app there is some actions that they do not need a keyEquivalent like Xcode app:

enter image description here

So I tried this below code for solving my problem:

let showWindow1 = NSMenuItem()
showWindow1.title = "Show Window 1"

This code Adds an item but it is grayout and it is not clickable any more from other side I do not know the way to assign the selector for it without having to give keyEquivalent for it.


Solution

  • Just pass an empty string instead of a key:

    let showWindow1 = NSMenuItem(title: "Show Window 1", action: #selector(showWindow1), keyEquivalent: "")