Skip to content
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

Support NSMenu on macOS 14 #136

Merged
merged 6 commits into from
Jul 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions Sources/KeyboardShortcuts/CarbonKeyboardShortcuts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ enum CarbonKeyboardShortcuts {
EventTypeSpec(eventClass: OSType(kEventClassKeyboard), eventKind: UInt32(kEventRawKeyUp))
]

// `keyDown` not working
private static let keyEventMonitor = LocalEventMonitor(events: [.keyDown, .keyUp]) { event in
private static let keyEventMonitor = RunLoopLocalEventMonitor(events: [.keyDown, .keyUp], runLoopMode: .eventTracking) { event in
guard
let eventRef = OpaquePointer(event.eventRef),
handleRawKeyEvent(eventRef) == noErr
Expand Down
54 changes: 54 additions & 0 deletions Sources/KeyboardShortcuts/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,60 @@ final class LocalEventMonitor {
}


final class RunLoopLocalEventMonitor {
private let runLoopMode: RunLoop.Mode
private let callback: (NSEvent) -> NSEvent?
private let observer: CFRunLoopObserver

init(
events: NSEvent.EventTypeMask,
runLoopMode: RunLoop.Mode,
callback: @escaping (NSEvent) -> NSEvent?
) {
self.runLoopMode = runLoopMode
self.callback = callback

self.observer = CFRunLoopObserverCreateWithHandler(nil, CFRunLoopActivity.beforeSources.rawValue, true, 0) { _, _ in
var eventsToHandle = [NSEvent]()

while let eventToHandle = NSApp.nextEvent(matching: .any, until: nil, inMode: .default, dequeue: true) {
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
eventsToHandle.append(eventToHandle)
}

for eventToHandle in eventsToHandle {
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
var handledEvent: NSEvent?

if !events.contains(NSEvent.EventTypeMask(rawValue: 1 << eventToHandle.type.rawValue)) {
handledEvent = eventToHandle
} else if let callbackEvent = callback(eventToHandle) {
handledEvent = callbackEvent
}

guard let handledEvent else {
continue
}

NSApp.postEvent(handledEvent, atStart: false)
}
}
}

deinit {
stop()
}

@discardableResult
func start() -> Self {
CFRunLoopAddObserver(RunLoop.current.getCFRunLoop(), observer, CFRunLoopMode(runLoopMode.rawValue as CFString))
return self
}

func stop() {
CFRunLoopRemoveObserver(RunLoop.current.getCFRunLoop(), observer, CFRunLoopMode(runLoopMode.rawValue as CFString))
}
}


extension NSEvent {
static var modifiers: ModifierFlags {
modifierFlags
Expand Down