macosappkitnsevent

AppKit Reading Keyboard Input in Separate Thread


Is there anyway to read keyboard inputs in AppKit off the main thread? I've managed to do so using CGEventTapCreate but unfortunately it requires permissions I'd rather not have to request. Is there a way to read local inputs off the main thread?

(Use case is a keyboard driven real time audio application where graphics should never block the input->audio pipeline)

I've tried using addLocalMonitorForEventsMatchingMask but it seems the handler is only called on the main thread even if I setup a runloop in a separate thread. Thanks in advance!


Solution

  • I believe the answer is 'no' on the basis that the entryway for keyboard input — assuming you don't try to misappropriate the accessibility event tap route — is either -[NSApplication sendEvent:] or your override of the same and per Apple "sendEvent: messages are sent from the main event loop (the run method)".

    It'll likely be a heavier lift but probably the appropriate solution here is to move your graphics off the main thread. Or at least break that work up into very small chunks so that you can route out from sendEvent:.