Re: Cocoa equivalent for 'InstallApplicationEventHandler'
Re: Cocoa equivalent for 'InstallApplicationEventHandler'
- Subject: Re: Cocoa equivalent for 'InstallApplicationEventHandler'
- From: Jesper Storm Bache <email@hidden>
- Date: Sat, 20 Feb 2010 09:51:27 -0800
- Acceptlanguage: en-US
- Thread-topic: Cocoa equivalent for 'InstallApplicationEventHandler'
Your comment only pertains to the addGlobal...
You can use addLocalMonitor to only snoop on events in your process. From the header:
=====
Use +addLocal to install an event monitor that receives events before they are dispatched by -[NSApplication sendEvent:]. In this case, your block should either return a valid NSEvent (which may be the same as the incoming NSEvent, or may be a newly created NSEvent) to cause the event to be dispatched, or it may return nil to stop dispatching of the event. Note that 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; only events that are dispatched through -[NSApplication sendEvent:] will be passed to your handler.
=====
addLocalMonitor is 10.6 only and you can view it as a somewhat similar to installing a handler on the Carbon event dispatcher.
If you want a solution that works on systems before 10.6, consider either overriding NSApplication sendEvent, or using a local (process specific) event tap.
Jesper
On Feb 20, 2010, at 4:08 AM, Uli Kusterer wrote:
> On 20.02.2010, at 12:53, Tom Davie wrote:
>> You want NSEvent's
>>
>> + (id)addGlobalMonitorForEventsMatchingMask:(NSEventMask)*mask* handler:(void
>> (^)(NSEvent*))*block*
>> *and*
>> *
>>
>> + (id)addLocalMonitorForEventsMatchingMask:(NSEventMask)mask handler:(NSEvent*
>> (^)(NSEvent*))block
>
> Nope, that's the wrong choice. This would give him copies of events sent to *other* applications as well (hence the "global").
>
> The sendEvent override seems like the most workable solution to me so far. Though I'd suggest not doing the work directly in your application instance. Instead, forward the event to your delegate. That way, you can use the same NSApplication subclass in your other apps.
>
> Cheers,
> -- Uli Kusterer
> "The Witnesses of TeachText are everywhere..."
> http://groups.yahoo.com/group/mac-gui-dev/
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden