3

I'm playing around a bit with global hot keys in Carbon, and I registered a hot key. When the hot key is pressed, this function is called:

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

This function is in the same file as my app delegate's definition. How do I now call an ObjC method on my delegate in that function?

[self aMethod];

I cant add a void* (to be a pointer to my class instance) to the C function, because I am not the one calling it.

I tried to convert the method into a class method and then call it with [AppDelegate aFunction], which works but then I cant use my instance variables!

3 Answers 3

8

You're reinventing the wheel. Use a wrapper:

http://github.com/davedelong/DDHotKey

well perhaps not a wheel, since hot keys aren't "trivial" concepts. maybe you're reinventing a differential? or something? ;)

Sign up to request clarification or add additional context in comments.

Comments

5

Give the pointer to your class instance as parameter for userData when you register your event handler. You will then get this pointer back inside the handler as userData.

Comments

3

Example code

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.