Sanich
Sanich

Reputation: 1835

Identifying AppleEvents in main message loop

I have a main message loop. I'm looking for a way to check if the event is an AppleEvent and if it's Event Class is of 'MyClass' then do something. I've looked in NSEvent reference and was lost not finding what i need. Please can someone suggest an approach?

while (!shutdown_now_) 
    {
        NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
        NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask
                                            untilDate:[NSDate distantFuture]
                                               inMode:NSDefaultRunLoopMode
                                              dequeue:YES];

        //if event is AppleEvent AND event class is <MyEventClass> then do something

        if (event) [NSApp sendEvent:event];
        [pool drain];
}  

Upvotes: 1

Views: 226

Answers (1)

deimus
deimus

Reputation: 9873

You cannot get an Apple Event via NSEvent in your loop like that.

Because NSEvent just doesn't cover it.

Documentation says

Almost all events in a Cocoa application are represented by objects of the NSEvent class. (Exceptions include Apple events, notifications, and similar items.) Each NSEvent object more narrowly represents a particular type of event, each with its own requirements for handling. The following sections describe the characteristics of an NSEvent object and the possible event types

You can find some more information in NSApplication documentation


Instead you can use the NSAppleEventManager class to register your own Apple event handlers with following method

- (void)setEventHandler:(id)handler
            andSelector:(SEL)handleEventSelector
          forEventClass:(AEEventClass)eventClass
             andEventID:(AEEventID)eventID

Upvotes: 1

Related Questions