NSMenuItem

extension NSMenuItem
  • Show a recorded keyboard shortcut in a NSMenuItem.

    The menu item will automatically be kept up to date with changes to the keyboard shortcut.

    Pass in nil to clear the keyboard shortcut.

    This method overrides .keyEquivalent and .keyEquivalentModifierMask.

    import Cocoa
    import KeyboardShortcuts
    
    extension KeyboardShortcuts.Name {
        static let toggleUnicornMode = Self("toggleUnicornMode")
    }
    
    // … `Recorder` logic for recording the keyboard shortcut …
    
    let menuItem = NSMenuItem()
    menuItem.title = "Toggle Unicorn Mode"
    menuItem.setShortcut(for: .toggleUnicornMode)
    

    You can test this method in the example project. Run it, record a shortcut and then look at the “Test” menu in the app’s main menu.

    Important

    You will have to disable the global keyboard shortcut while the menu is open, as otherwise, the keyboard events will be buffered up and triggered when the menu closes. This is because NSMenu puts the thread in tracking-mode, which prevents the keyboard events from being received. You can listen to whether a menu is open by implementing NSMenuDelegate#menuWillOpen and NSMenuDelegate#menuDidClose. You then use KeyboardShortcuts.disable and KeyboardShortcuts.enable.

    Declaration

    Swift

    public func setShortcut(for name: KeyboardShortcuts.Name?)
  • Add a keyboard shortcut to a NSMenuItem.

    This method is only recommended for dynamic shortcuts. In general, it’s preferred to create a static shortcut name and use NSMenuItem.setShortcut(for:) instead.

    Pass in nil to clear the keyboard shortcut.

    This method overrides .keyEquivalent and .keyEquivalentModifierMask.

    Important

    You will have to disable the global keyboard shortcut while the menu is open, as otherwise, the keyboard events will be buffered up and triggered when the menu closes. This is because NSMenu puts the thread in tracking-mode, which prevents the keyboard events from being received. You can listen to whether a menu is open by implementing NSMenuDelegate#menuWillOpen and NSMenuDelegate#menuDidClose. You then use KeyboardShortcuts.disable and KeyboardShortcuts.enable.

    Declaration

    Swift

    @_disfavoredOverload
    public func setShortcut(_ shortcut: KeyboardShortcuts.Shortcut?)