Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mark the NSEvent as handled?

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?

like image 833
Sergei Moskvin Avatar asked Dec 02 '25 20:12

Sergei Moskvin


1 Answers

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.

like image 141
Rob Napier Avatar answered Dec 05 '25 09:12

Rob Napier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!