Access ObjC object from C function callback

I played around a bit with global hotkeys in Carbon and I registered a hotkey. When a hot key is pressed, this function is called:

OSStatus myHotKeyHandler(EventHandlerCallRef nextHandler, EventRef anEvent, void *userData) {
    ...
}

This function is in the same file as the delegate definition of my application. How do I now name the ObjC method for my delegate in this function?

[self aMethod];

I cannot add void*(to be a pointer to an instance of a class) a C function, because I'm not the one who calls it.

I tried to convert the method to a class method, and then call it using [AppDelegate aFunction]that works, but then I can not use the instance variables!

+3
source share
3 answers

userData, . userData.

+5
+8

Code example

InstallApplicationEventHandler(&MyHotKeyHandler,1,&eventType,(void *)self,NULL);
...

OSStatus MyHotKeyHandler(EventHandlerCallRef nextHandler,EventRef theEvent,
                         void *userData)
{
    [(yourAppDelegate *)userData dosomething];
}
+3
source

All Articles