Now returns a (discardable) Bool for registering and/or enabling shortcuts, so we know if it worked or not.
This commit is contained in:
parent
fe481cb687
commit
b2193a2037
|
@ -37,12 +37,12 @@ enum CarbonKeyboardShortcuts {
|
|||
private static var hotKeyId = 0
|
||||
private static var eventHandler: EventHandlerRef?
|
||||
|
||||
private static func setUpEventHandlerIfNeeded() {
|
||||
private static func setUpEventHandlerIfNeeded() -> Bool {
|
||||
guard
|
||||
eventHandler == nil,
|
||||
let dispatcher = GetEventDispatcherTarget()
|
||||
else {
|
||||
return
|
||||
return false
|
||||
}
|
||||
|
||||
let eventSpecs = [
|
||||
|
@ -50,21 +50,23 @@ enum CarbonKeyboardShortcuts {
|
|||
EventTypeSpec(eventClass: OSType(kEventClassKeyboard), eventKind: UInt32(kEventHotKeyReleased))
|
||||
]
|
||||
|
||||
InstallEventHandler(
|
||||
guard InstallEventHandler(
|
||||
dispatcher,
|
||||
carbonKeyboardShortcutsEventHandler,
|
||||
eventSpecs.count,
|
||||
eventSpecs,
|
||||
nil,
|
||||
&eventHandler
|
||||
)
|
||||
) == noErr else { return false }
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
static func register(
|
||||
@discardableResult static func register(
|
||||
_ shortcut: KeyboardShortcuts.Shortcut,
|
||||
onKeyDown: @escaping (KeyboardShortcuts.Shortcut) -> Void,
|
||||
onKeyUp: @escaping (KeyboardShortcuts.Shortcut) -> Void
|
||||
) {
|
||||
) -> Bool {
|
||||
hotKeyId += 1
|
||||
|
||||
var eventHotKey: EventHotKeyRef?
|
||||
|
@ -81,7 +83,7 @@ enum CarbonKeyboardShortcuts {
|
|||
registerError == noErr,
|
||||
let carbonHotKey = eventHotKey
|
||||
else {
|
||||
return
|
||||
return false
|
||||
}
|
||||
|
||||
hotKeys[hotKeyId] = HotKey(
|
||||
|
@ -92,7 +94,7 @@ enum CarbonKeyboardShortcuts {
|
|||
onKeyUp: onKeyUp
|
||||
)
|
||||
|
||||
setUpEventHandlerIfNeeded()
|
||||
return setUpEventHandlerIfNeeded()
|
||||
}
|
||||
|
||||
private static func unregisterHotKey(_ hotKey: HotKey) {
|
||||
|
|
|
@ -37,18 +37,22 @@ public enum KeyboardShortcuts {
|
|||
*/
|
||||
static var isPaused = false
|
||||
|
||||
private static func register(_ shortcut: Shortcut) {
|
||||
@discardableResult private static func register(_ shortcut: Shortcut) -> Bool {
|
||||
guard !registeredShortcuts.contains(shortcut) else {
|
||||
return
|
||||
return true //already registered
|
||||
}
|
||||
|
||||
CarbonKeyboardShortcuts.register(
|
||||
let keyboardShortcutRegistered = CarbonKeyboardShortcuts.register(
|
||||
shortcut,
|
||||
onKeyDown: handleOnKeyDown,
|
||||
onKeyUp: handleOnKeyUp
|
||||
)
|
||||
|
||||
guard keyboardShortcutRegistered else { return false }
|
||||
|
||||
registeredShortcuts.insert(shortcut)
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -129,12 +133,12 @@ public enum KeyboardShortcuts {
|
|||
/**
|
||||
Enable a disabled keyboard shortcut.
|
||||
*/
|
||||
public static func enable(_ name: Name) {
|
||||
@discardableResult public static func enable(_ name: Name) -> Bool {
|
||||
guard let shortcut = getShortcut(for: name) else {
|
||||
return
|
||||
return false
|
||||
}
|
||||
|
||||
register(shortcut)
|
||||
return register(shortcut)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue