prole/prole-app/Sources/HotKeyManager.swift
chrisfu 197c72dd7a Refactor prole-app and establish temporary release process
- Moved prole-tools-app to prole-app at the project root to make it self-contained for transition to its own repository.
- Created prole-tools-app/dist/ directory to host build artifacts.
- Generated distribution artifacts (Prole Tools.app and Prole Tools.zip) using prole-app/build.sh package.
- Checked in the generated artifacts to prole-tools-app/dist/ (bypassing .gitignore for temporary release process).
Changes Summary
•
Renamed directory prole-tools-app/ to prole-app/.
•
Populated prole-tools-app/dist/ with the latest build output from prole-app/build.sh.
•
Staged all changes, including the forced addition of ignored artifacts in prole-tools-app/dist/.
2026-01-18 15:04:23 -08:00

35 lines
1.5 KiB
Swift

import AppKit
import Carbon.HIToolbox
final class HotKeyManager {
private var hotKeyRef: EventHotKeyRef?
enum Key: UInt32 { case p = 35 /* US keyboard virtual keycode for P */ }
struct Modifiers: OptionSet {
let rawValue: UInt32
static let command = Modifiers(rawValue: UInt32(cmdKey))
static let option = Modifiers(rawValue: UInt32(optionKey))
static let shift = Modifiers(rawValue: UInt32(shiftKey))
}
func registerGlobalHotKey(modifiers: Modifiers, key: Key, handler: @escaping () -> Void) {
var eventSpec = EventTypeSpec(eventClass: OSType(kEventClassKeyboard), eventKind: UInt32(kEventHotKeyPressed))
InstallEventHandler(GetApplicationEventTarget(), { (_, _, userData) -> OSStatus in
let handler = Unmanaged<HotKeyHandlerWrapper>.fromOpaque(userData!).takeUnretainedValue()
handler.handler()
return noErr
}, 1, &eventSpec, Unmanaged.passRetained(HotKeyHandlerWrapper(handler: handler)).toOpaque(), nil)
let hotKeyID = EventHotKeyID(signature: OSType(UInt32(bitPattern: Int32(bitPattern: 0x50524F4C))), id: 1) // 'PROL'
RegisterEventHotKey(UInt32(key.rawValue), modifiers.rawValue, hotKeyID, GetApplicationEventTarget(), 0, &hotKeyRef)
}
deinit { if let ref = hotKeyRef { UnregisterEventHotKey(ref) } }
}
private final class HotKeyHandlerWrapper {
let handler: () -> Void
init(handler: @escaping () -> Void) { self.handler = handler }
}