-
-
Notifications
You must be signed in to change notification settings - Fork 2
Add global hotkeys for toggling MiddleDrag and menu bar visibility #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a0618aa
Add global hotkeys for toggling MiddleDrag and menu bar visibility
NullPointerDepressiveDisorder e211237
Implement hotkey customization and recording functionality
NullPointerDepressiveDisorder 5c6fb73
Add applicationShouldHandleReopen method and update menu item text
NullPointerDepressiveDisorder d0b2b66
Implement cleanup for hotkey recording and improve alert handling
NullPointerDepressiveDisorder faaefbc
Refactor multitouch and hotkey handling for improved safety and funct…
NullPointerDepressiveDisorder 263a4d2
Refactor menu bar visibility handling and enhance hotkey tests
NullPointerDepressiveDisorder e2ef448
Enhance MenuBarController to skip button click during tests
NullPointerDepressiveDisorder d3f3865
Refactor MenuBarController hotkey handling for improved safety
NullPointerDepressiveDisorder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| // | ||
| // GlobalHotKeyManager.swift | ||
| // MiddleDrag | ||
| // | ||
|
|
||
| import Cocoa | ||
| import Carbon.HIToolbox | ||
|
|
||
| /// Manages system-wide hotkeys using Carbon's RegisterEventHotKey | ||
| /// Threading: Delivers handlers on the main thread | ||
| @safe @MainActor | ||
NullPointerDepressiveDisorder marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public final class GlobalHotKeyManager { | ||
| public static let shared = GlobalHotKeyManager() | ||
|
|
||
| // Map hotkey IDs to handlers | ||
| private var handlers: [UInt32: () -> Void] = [:] | ||
| private var hotKeyRefs: [UInt32: EventHotKeyRef?] = unsafe [:] | ||
| private var nextID: UInt32 = 1 | ||
|
|
||
| // Keep a reference to the installed event handler | ||
| private var eventHandler: EventHandlerRef? | ||
|
|
||
| // Unique signature to identify our hotkeys (any 4-byte code) | ||
| private let signature: OSType = 0x4D44484B // 'MDHK' | ||
|
|
||
| private init() { | ||
| // Install a single event handler for all hotkeys we register | ||
| var eventType = EventTypeSpec(eventClass: OSType(kEventClassKeyboard), | ||
| eventKind: UInt32(kEventHotKeyPressed)) | ||
|
|
||
| let callback: EventHandlerUPP = { (_, eventRef, userData) in | ||
| // Extract the EventHotKeyID for the pressed hotkey | ||
| var hotKeyID = EventHotKeyID() | ||
| let status = unsafe GetEventParameter(eventRef, | ||
| EventParamName(kEventParamDirectObject), | ||
| EventParamType(typeEventHotKeyID), | ||
| nil, | ||
| MemoryLayout.size(ofValue: hotKeyID), | ||
| nil, | ||
| &hotKeyID) | ||
| guard status == noErr else { return noErr } | ||
|
|
||
| // Bridge back to Swift instance | ||
| if let userData = unsafe userData { | ||
| let manager = unsafe Unmanaged<GlobalHotKeyManager> | ||
| .fromOpaque(userData) | ||
| .takeUnretainedValue() | ||
| let id = hotKeyID.id | ||
| if let handler = manager.handlers[id] { | ||
| // Deliver on main thread to safely call AppKit/UI code | ||
| DispatchQueue.main.async { | ||
| handler() | ||
| } | ||
| } | ||
| } | ||
| return noErr | ||
| } | ||
|
|
||
| unsafe InstallEventHandler(GetEventDispatcherTarget(), | ||
| callback, | ||
| 1, | ||
| &eventType, | ||
| UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque()), | ||
| &eventHandler) | ||
| } | ||
|
|
||
| func invalidate() { | ||
| // Unregister all hotkeys and remove the handler | ||
| for unsafe (_, ref) in unsafe hotKeyRefs { | ||
| if let ref = unsafe ref { unsafe UnregisterEventHotKey(ref) } | ||
| } | ||
| unsafe hotKeyRefs.removeAll() | ||
|
|
||
| if let handler = unsafe eventHandler { | ||
| unsafe RemoveEventHandler(handler) | ||
| unsafe eventHandler = nil | ||
| } | ||
| } | ||
|
|
||
| /// Register a global hotkey | ||
| /// - Parameters: | ||
| /// - keyCode: A virtual key code (e.g. kVK_ANSI_E) | ||
| /// - modifiers: Carbon modifier mask (e.g. cmdKey | shiftKey) | ||
| /// - handler: Closure invoked when the hotkey is pressed | ||
| /// - Returns: An identifier to later unregister if needed | ||
| @discardableResult | ||
| public func register(keyCode: UInt32, modifiers: UInt32, handler: @escaping () -> Void) -> UInt32 { | ||
| let id = nextID | ||
| nextID &+= 1 | ||
|
|
||
| var ref: EventHotKeyRef? | ||
| let hotKeyID = EventHotKeyID(signature: signature, id: id) | ||
|
|
||
| let status = unsafe RegisterEventHotKey(keyCode, | ||
| modifiers, | ||
| hotKeyID, | ||
| GetEventDispatcherTarget(), | ||
| 0, | ||
| &ref) | ||
|
|
||
| guard status == noErr, let _ = unsafe ref else { | ||
| // Registration can fail if another app already claimed the combo | ||
| NSLog("GlobalHotKeyManager: Failed to register hotkey (code \(keyCode), mods \(modifiers))") | ||
| return 0 | ||
| } | ||
|
|
||
| unsafe hotKeyRefs[id] = unsafe ref | ||
| handlers[id] = handler | ||
| return id | ||
| } | ||
|
|
||
| /// Unregister a previously registered hotkey by ID | ||
| func unregister(id: UInt32) { | ||
| if let ref = unsafe hotKeyRefs[id] { | ||
| if let ref = unsafe ref { unsafe UnregisterEventHotKey(ref) } | ||
| unsafe hotKeyRefs[id] = nil | ||
| } | ||
| handlers[id] = nil | ||
| } | ||
|
|
||
| /// Utility: Convert NSEvent.ModifierFlags to Carbon modifiers | ||
| public static func carbonModifiers(from flags: NSEvent.ModifierFlags) -> UInt32 { | ||
| var result: UInt32 = 0 | ||
| if flags.contains(.command) { result |= UInt32(cmdKey) } | ||
| if flags.contains(.option) { result |= UInt32(optionKey) } | ||
| if flags.contains(.shift) { result |= UInt32(shiftKey) } | ||
| if flags.contains(.control) { result |= UInt32(controlKey) } | ||
| return result | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.