From 935d9eb63db6bd37342df614caed652578fa690d Mon Sep 17 00:00:00 2001 From: decodism Date: Mon, 20 Feb 2023 12:57:45 +0100 Subject: [PATCH] Add isEnabled --- .../CarbonKeyboardShortcuts.swift | 16 +++++++++++----- .../KeyboardShortcuts/KeyboardShortcuts.swift | 13 +++++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Sources/KeyboardShortcuts/CarbonKeyboardShortcuts.swift b/Sources/KeyboardShortcuts/CarbonKeyboardShortcuts.swift index a57c0ff..98bcd44 100644 --- a/Sources/KeyboardShortcuts/CarbonKeyboardShortcuts.swift +++ b/Sources/KeyboardShortcuts/CarbonKeyboardShortcuts.swift @@ -81,14 +81,20 @@ enum CarbonKeyboardShortcuts { return } - if KeyboardShortcuts.isMenuOpen { + if KeyboardShortcuts.isEnabled { + if KeyboardShortcuts.isMenuOpen { + softUnregisterAll() + RemoveEventTypesFromHandler(eventHandler, hotKeyEventTypes.count, hotKeyEventTypes) + AddEventTypesToHandler(eventHandler, rawKeyEventTypes.count, rawKeyEventTypes) + } else { + softRegisterAll() + RemoveEventTypesFromHandler(eventHandler, rawKeyEventTypes.count, rawKeyEventTypes) + AddEventTypesToHandler(eventHandler, hotKeyEventTypes.count, hotKeyEventTypes) + } + } else { softUnregisterAll() RemoveEventTypesFromHandler(eventHandler, hotKeyEventTypes.count, hotKeyEventTypes) - AddEventTypesToHandler(eventHandler, rawKeyEventTypes.count, rawKeyEventTypes) - } else { - softRegisterAll() RemoveEventTypesFromHandler(eventHandler, rawKeyEventTypes.count, rawKeyEventTypes) - AddEventTypesToHandler(eventHandler, hotKeyEventTypes.count, hotKeyEventTypes) } } diff --git a/Sources/KeyboardShortcuts/KeyboardShortcuts.swift b/Sources/KeyboardShortcuts/KeyboardShortcuts.swift index 7e243c7..9e498f8 100644 --- a/Sources/KeyboardShortcuts/KeyboardShortcuts.swift +++ b/Sources/KeyboardShortcuts/KeyboardShortcuts.swift @@ -37,6 +37,19 @@ public enum KeyboardShortcuts { */ static var isPaused = false + /** + Enable/disable monitoring of all keyboard shortcuts. + */ + public static var isEnabled = true { + didSet { + guard isEnabled != oldValue else { + return + } + + CarbonKeyboardShortcuts.updateEventHandler() + } + } + /** Set according to the opening state of your NSMenu if you want your keyboard shortcuts to work when it is open.