You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// TODO: Make this a getter/setter. We must first add the ability to create a `Shortcut` from a `keyEquivalent`.
9
14
/**
10
15
Show a recorded keyboard shortcut in a `NSMenuItem`.
@@ -35,20 +40,48 @@ extension NSMenuItem {
35
40
- 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`.
AssociatedKeys.observer[self]=NotificationCenter.default.addObserver(forName:.shortcutByNameDidChange, object:nil, queue:nil){ notification in
58
+
guard
59
+
let nameInNotification = notification.userInfo?["name"]as?KeyboardShortcuts.Name,
60
+
nameInNotification == name
61
+
else{
62
+
return
63
+
}
64
+
65
+
set()
66
+
}
67
+
}
68
+
69
+
/**
70
+
Add a keyboard shortcut to a `NSMenuItem`.
71
+
72
+
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.
73
+
74
+
Pass in `nil` to clear the keyboard shortcut.
75
+
76
+
This method overrides `.keyEquivalent` and `.keyEquivalentModifierMask`.
77
+
78
+
- 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`.
0 commit comments