Reputation: 129
I handle pressing of letter keys on keyboard with the following code.
NSEvent.addGlobalMonitorForEvents(matching: NSEvent.EventTypeMask.keyDown) { event in
// code
}
How do I then mark the NSEvent as handled, not allowing it to be handled again by system and another apps? For example do not allow the input of a pressed letter in the opened text editor?
Upvotes: 3
Views: 668
Reputation: 299275
You can't do that with NSEvent
. As the docs note:
Events are delivered asynchronously to your app and you can only observe the event; you cannot modify or otherwise prevent the event from being delivered to its original target application.
You're not part of the event-generation system; you're just getting notifications as part of your runloop.
If you want to become part of the event system, below the app layer, you need to use CGEvent
. See tapCreate(tap:place:options:eventsOfInterest:callback:userInfo:)
. The callback can return NULL
to indicate it's consumed the event.
Upvotes: 5