Returning nil from addLocalMonitorForEventsMatchingMask: block doesn't suppress events
-
09-03-2021 - |
Domanda
The following code doesn't suppress mouse drags in my app:
[NSEvent addLocalMonitorForEventsMatchingMask:NSLeftMouseDraggedMask handler:^(NSEvent *event) {
return (NSEvent *)nil;
}];
I'm still able to drag UI elements inside the app. As far as I know, if you return nil
inside the block, the event won't be dispatched to the app. What am I missing?
Soluzione
I'm not quite sure what you mean by "drag UI elements", but I suspect that this note in the docs:
Your handler will not be called for events that are consumed by nested event-tracking loops such as control tracking, menu tracking, or window dragging...
is the explanation. Various NSResponder
subclasses, such as buttons when they get clicked, enter a "private tracking loop" where they grab all events until they receive one in particular which ends the loop.
You should put a log statement inside that block and make sure that you're actually getting an event passed in when you expect to.