35 lines
905 B
Swift
35 lines
905 B
Swift
import Carbon
|
|
import AppKit
|
|
|
|
final class HotkeyManager {
|
|
static let shared = HotkeyManager()
|
|
|
|
var onTrigger: (() -> Void)?
|
|
|
|
private var hotKeyRef: EventHotKeyRef?
|
|
private var eventHandlerRef: EventHandlerRef?
|
|
private var hotKeyID = EventHotKeyID(signature: 0x4B505452, id: 1)
|
|
|
|
private init() {}
|
|
|
|
func register() {
|
|
var eventType = EventTypeSpec(
|
|
eventClass: OSType(kEventClassKeyboard),
|
|
eventKind: UInt32(kEventHotKeyPressed)
|
|
)
|
|
InstallEventHandler(GetApplicationEventTarget(), { _, _, _ in
|
|
HotkeyManager.shared.onTrigger?()
|
|
return noErr
|
|
}, 1, &eventType, nil, &eventHandlerRef)
|
|
|
|
RegisterEventHotKey(
|
|
UInt32(kVK_ANSI_S),
|
|
UInt32(cmdKey | shiftKey),
|
|
hotKeyID,
|
|
GetApplicationEventTarget(),
|
|
0,
|
|
&hotKeyRef
|
|
)
|
|
}
|
|
}
|