-
-
Notifications
You must be signed in to change notification settings - Fork 237
Fix option+letter shortcuts being intercepted by IME layer #236
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,6 +117,8 @@ final class HotKeyCenter { | |
| return nil | ||
| } | ||
|
|
||
| private var globalEventMonitor: AnyObject? | ||
|
|
||
| var mode: Mode = .normal { | ||
| didSet { | ||
| guard mode != oldValue else { | ||
|
|
@@ -219,6 +221,7 @@ final class HotKeyCenter { | |
| } | ||
|
|
||
| hotKeys.removeValue(forKey: hotKey.id) | ||
| updateEventHandler() | ||
| } | ||
|
|
||
| private func pause(_ hotKey: HotKey) { | ||
|
|
@@ -324,6 +327,38 @@ final class HotKeyCenter { | |
|
|
||
| setHotKeyEventHandlingEnabled(shouldHandleHotKeys) | ||
| setRawKeyEventHandlingEnabled(shouldHandleRawKeys) | ||
| setGlobalOptionKeyMonitorEnabled(mode == .normal && hasOptionOnlyShortcuts) | ||
| } | ||
|
|
||
| /** | ||
| Returns true if any registered shortcut uses Option as the only modifier (no Command or Control). | ||
| Such shortcuts are prone to being intercepted by the IME layer before Carbon receives them. | ||
| */ | ||
| private var hasOptionOnlyShortcuts: Bool { | ||
| hotKeys.values.compactMap(\.value).contains { hotKey in | ||
| let modifiers = NSEvent.ModifierFlags(carbon: hotKey.carbonModifiers) | ||
| return modifiers.contains(.option) | ||
| && !modifiers.contains(.command) | ||
| && !modifiers.contains(.control) | ||
| } | ||
| } | ||
|
Comment on lines
+333
to
+342
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It won't work if a user has also shortcuts with different modifiers. It would be best to combine both the current solution and the new one, I guess.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe your application is more important and should run globally. |
||
|
|
||
| private func setGlobalOptionKeyMonitorEnabled(_ isEnabled: Bool) { | ||
| if isEnabled, globalEventMonitor == nil { | ||
| globalEventMonitor = NSEvent.addGlobalMonitorForEvents(matching: [.keyDown, .keyUp]) { [weak self] event in | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This requires accessibility access, so it's a no-go. From docs:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Many apps use accessibility API. How about making it optional? Clients could decide wheter they want to use this API or not. |
||
| guard | ||
| let self, | ||
| let eventRef = OpaquePointer(event.eventRef) | ||
| else { | ||
| return | ||
| } | ||
|
|
||
| _ = handleRawKeyEvent(eventRef) | ||
| } | ||
| } else if !isEnabled, let monitor = globalEventMonitor { | ||
| NSEvent.removeMonitor(monitor) | ||
| globalEventMonitor = nil | ||
| } | ||
| } | ||
|
|
||
| private func setHotKeyEventHandlingEnabled(_ isEnabled: Bool) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably a more appropriate check would be:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed