Re: creating a cocoa nsapp event polling callback

  • On 26 Sep 2007, at 08:38, Tobias Ford wrote:

    > I'd like to give my NSApp a callback function that will process
    > specific events such as keys and mouse events before they have the
    > chance to be processed by the Cocoa frameworks.  Anything that I
    > don't process will then be sent back to NSApplication for standard
    > processing.
    >
    > I think that my only option is to overload the run method.  I don't
    > really want to do this due to future maintenance troubles.  What
    > I'm looking for is some method that is called pre-standard-event
    > processing that I can overload poll my events, send unused events
    > back to the app, and then call an update function from.  I want
    > something that isn't likely to break with an os update and is public.
    >
    > I guess I'm looking something like this:
    >
    > - (void)
    > publicMethodCalledBeforePollingNSApplicationsNextEventsPerFrame;
    >
    > Is there anything like this?

    How about overriding NSApplication's -sendEvent:?

    Kind regards,

    Alastair.

    --
    http://alastairs-place.net
  • To be clear, change your default application's NSPrincipalClass in
    Info.plist to a custom subclass of NSApplication and override sendEvent:
    @interface MyApplication : NSApplication

    @end

    @implementation MyApplication

    - (void)sendEvent:(NSEvent *)event
    {
        if(IWantToProcessThisEventMySelf(event)) {
            // ...
        } else {
            [super sendEvent:event];
        }
    }

    @end
previous month september 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Go to today