Skip to content

Commit 32f7939

Browse files
committed
* 'main' of https://github.com/sindresorhus/KeyboardShortcuts: Tweaks Add `isEnabled()` method (sindresorhus#215) # Conflicts: # Sources/KeyboardShortcuts/KeyboardShortcuts.swift
2 parents c5fda33 + 2927f70 commit 32f7939

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

Sources/KeyboardShortcuts/KeyboardShortcuts.swift

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public enum KeyboardShortcuts {
193193

194194
This can be used to reset the handlers before re-creating them to avoid having multiple handlers for the same shortcut.
195195

196-
- Note: This method does not affect listeners using `.on()`.
196+
- Note: This method does not affect listeners using ``events(for:)``.
197197
*/
198198
public static func removeAllHandlers() {
199199
let shortcutsToUnregister = shortcutsForLegacyHandlers.subtracting(shortcutsForStreamHandlers)
@@ -241,7 +241,7 @@ public enum KeyboardShortcuts {
241241

242242
- Parameter name: The name of the keyboard shortcut to remove handlers for.
243243

244-
- Note: This method does not affect listeners using `.on()`.
244+
- Note: This method does not affect listeners using ``events(for:)``.
245245
*/
246246
public static func removeHandler(for name: Name) {
247247
legacyKeyDownHandlers[name] = nil
@@ -258,6 +258,28 @@ public enum KeyboardShortcuts {
258258
unregister(shortcut)
259259
}
260260

261+
/**
262+
Returns whether the keyboard shortcut for the given name is enabled.
263+
264+
This checks if the shortcut is registered and will trigger handlers. It respects the global ``isEnabled``.
265+
266+
```swift
267+
let isEnabled = KeyboardShortcuts.isEnabled(for: .toggleUnicornMode)
268+
```
269+
270+
- Tip: Use ``disable(_:)-(Name...)`` and ``enable(_:)-(Name...)`` to change the status.
271+
*/
272+
public static func isEnabled(for name: Name) -> Bool {
273+
guard
274+
isEnabled,
275+
let shortcut = getShortcut(for: name)
276+
else {
277+
return false
278+
}
279+
280+
return registeredShortcuts.contains(shortcut)
281+
}
282+
261283
/**
262284
Disable the keyboard shortcut for one or more names.
263285
*/
@@ -469,6 +491,8 @@ public enum KeyboardShortcuts {
469491

470492
You can safely call this even if the user has not yet set a keyboard shortcut. It will just be inactive until they do.
471493

494+
- Important: This will be deprecated in the future. Prefer ``events(for:)`` for new code.
495+
472496
```swift
473497
import AppKit
474498
import KeyboardShortcuts
@@ -498,6 +522,8 @@ public enum KeyboardShortcuts {
498522

499523
You can safely call this even if the user has not yet set a keyboard shortcut. It will just be inactive until they do.
500524

525+
- Important: This will be deprecated in the future. Prefer ``events(for:)`` for new code.
526+
501527
```swift
502528
import AppKit
503529
import KeyboardShortcuts

Sources/KeyboardShortcuts/Shortcut.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -757,10 +757,18 @@ extension KeyboardShortcuts.Shortcut {
757757
let specialKey = keyToSpecialKeyMapping[key]
758758
{
759759
if let keyEquivalent = specialKey.swiftUIKeyEquivalent {
760-
return KeyboardShortcut(keyEquivalent, modifiers: modifiers.toEventModifiers)
760+
if #available(macOS 12.0, *) {
761+
return KeyboardShortcut(keyEquivalent, modifiers: modifiers.toEventModifiers, localization: .custom)
762+
} else {
763+
return KeyboardShortcut(keyEquivalent, modifiers: modifiers.toEventModifiers)
764+
}
761765
}
762766
} else if let character = keyToCharacter() {
763-
return KeyboardShortcut(KeyEquivalent(character), modifiers: modifiers.toEventModifiers)
767+
if #available(macOS 12.0, *) {
768+
return KeyboardShortcut(KeyEquivalent(character), modifiers: modifiers.toEventModifiers, localization: .custom)
769+
} else {
770+
return KeyboardShortcut(KeyEquivalent(character), modifiers: modifiers.toEventModifiers)
771+
}
764772
}
765773

766774
return nil

0 commit comments

Comments
 (0)