Skip to content

Instantly share code, notes, and snippets.

@Kentzo
Created March 28, 2020 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kentzo/f8289d99c5d93d81f5ca60b7adc94fae to your computer and use it in GitHub Desktop.
Save Kentzo/f8289d99c5d93d81f5ca60b7adc94fae to your computer and use it in GitHub Desktop.
Carbon API to handle Global Hot Keys should propagate hot key events as regular KeyDown / KeyUp events if not handled
Among the small subset of Carbon API that still exists and has no modern replacement is an API to register Global Hot Keys. This API works in 2 steps:
1. Install an Event Handler for GetEventDispatcherTarget() for kEventHotKeyPressed and kEventHotKeyReleased
2. Registering Hot Keys with RegisterEventHotKey for GetEventDispatcherTarget()
Once the registered key combination is pressed, custom Event Handler gets called. The handler can then either return a noErr indicating that the event was processed and does not need to be propagated further, return eventNotHandledErr indicating that event should be propagated or any other error.
This issue with this API is that once a keyboard event is recognized as a Hot Key it's not put back onto event queues by macOS as a regular KeyDown / KeyUp event. Not even when all installed event handlers return eventNotHandledErr.
The workaround is to construct the appropriate KeyDown / KeyUp and manually post it on an event queue. This, unfortunately, requires the app to have the Accessibility permission granted by the user which is a lot more permissive than necessary.
My request is to have an API that would allow to instruct the system to re-insert the Hot Key event as a regular KeyDown / KeyUp event if all Global Hot Key handlers return eventNotHandledErr.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment