sindresorhus/KeyboardShortcuts

keyboardshortcuts shows wrong key on the NSMenu

pradeepb28 opened this issue · 1 comments

I have a shortcut that I had set with option+tab to open an app, it works accordingly but on the UI it shows command + option symbols.

Settings
CleanShot 2022-12-17 at 12 11 42

NSMenu
CleanShot 2022-12-17 at 12 11 14

I used KeyboardShortcuts.getShorcut(...).description to assign the value on the UI

When I simply use print statement, it shows correct symbols.

Conclusion: On the UI it shows different symbol but functionally it works as expected as option+tab commands

That is the wrong way to do it. See:

/**
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`.
*/