diff --git a/Sources/KeyboardShortcuts/KeyboardShortcuts.swift b/Sources/KeyboardShortcuts/KeyboardShortcuts.swift index 105c465..04c5fd8 100644 --- a/Sources/KeyboardShortcuts/KeyboardShortcuts.swift +++ b/Sources/KeyboardShortcuts/KeyboardShortcuts.swift @@ -181,6 +181,30 @@ public enum KeyboardShortcuts { legacyKeyUpHandlers = [:] } + /** + Remove the keyboard shortcut handler for the given name. + + This can be used to reset the handler before re-creating it to avoid having multiple handlers for the same shortcut. + + - Parameter name: The name of the keyboard shortcut to remove handlers for. + + - Note: This method does not affect listeners using `.on()`. + */ + public static func removeHandler(for name: Name) { + legacyKeyDownHandlers[name] = nil + legacyKeyUpHandlers[name] = nil + + // Make sure not to unregister stream handlers. + guard + let shortcut = getShortcut(for: name), + !shortcutsForStreamHandlers.contains(shortcut) + else { + return + } + + unregister(shortcut) + } + // TODO: Also add `.isEnabled(_ name: Name)`. /**