1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Calling a Cocoa method from Carbon code

Discussion in 'Mac Programming' started by AshleyByrom, Sep 5, 2010.

  1. macrumors newbie


    I'm trying to use global keyboard shortcuts inside my Cocoa app and, as far as I know, this can only be done with Carbon.

    I'm relatively new to Cocoa and Mac Programming as a whole (This being my first real application) and so, I'm EXTREMELY new to Carbon! From what I can understand Cocoa and Carbon are just two different APIs with Cocoa being the 'newer' one (OS X) and the 'preferred' one whereas Carbon is the 'older' (Pre OS X) but it still can do things Cocoa can't so it still gets used.

    ^^ I hope I understood it right, please correct me if I'm wrong!

    Anyway, I have a Cocoa app and I'm trying to reference a Cocoa method in Carbon code. The keyboard shortcuts work and the Carbon code I have is

    OSStatus MyHotKeyHandler(EventHandlerCallRef nextHandler,EventRef theEvent, void *userData) {
    	NSAutoreleasePool *localPool;
    	localPool = [[NSAutoreleasePool alloc] init];
    	EventHotKeyID hkCom;
    	int l = hkCom.id;
    	switch (l) {
    		case 1:
    			NSLog(@"Decrease shorcut!");
    		case 2:
    			NSLog(@"Increase shortcut!");
    	[localPool release];
    	return noErr;
    Like I said, that works. But I have two methods in my app controller (Cocoa) that I'd like to call instead of NSLog. they're called increaseBrightness and decreaseBrightness. I'd love to just be able to put [self increaseBrightness] instead of the NSLogs but I get errors. I've ready through the CarbonCocoa Integration Guide and it just seems really confusing! Any help?

  2. macrumors 68040


    You definitely can't use self if you aren't in a method associated with an Object. However, if you pass your controller to this method, passing messages to it should be fine. If the call of this method is not from your own code, so you can't add an argument, then you could write a function that returns the pointer to your controller and use that.

  3. macrumors newbie

    Thanks for the suggestions, I've just read somewhere that Carbon isn't supported in Snow Leopard. I know it'll be different to this but it's hard to find that actual answer of whether or not my code would work in SL?

    This is really annoying for me because I'm running Leopard currently and can't test it. Do you know if this code would run in SL?
  4. macrumors 68040


    Can you post where? This sort of change would be very drastic, and I'm confident that a number of apps I'm using on SL use carbon code. There are some APIs that simply don't have equivalents in Cocoa yet, and until that changes Carbon can't very well go away. I can't say for sure whether some particular code will work or not, but I can tell you Carbon is still available in Snow Leopard. Classic mode is not available as of 10.5, but that's different.

  5. macrumors newbie


    ^^ That post makes out that the Carbon way won't work and you have to use the new Cocoa way. But does this mean the Cocoa way won't work in Leopard or below?
  6. Moderator


    Staff Member

    Cabon is supported in SL. Carbon GUIs cannot use 64bit mode (whereas Cocoa ones can in SL).
  7. macrumors newbie

    Thankyou Robbie and Lee.

    Lee I used your suggestion and passed 'self' through to the function, this seems to of worked for me on 10.5.8. I just hope to God this works in SL!

    My only problem now is allowing the user to set the global hotkey themselves. I'm going to look for a way to do this, hopefully I'll find something. If any of you know a way I would really appreciate it but my question in this thread has been answered and I am greatful!
  8. macrumors newbie

    Please could post your code on how you did this

Share This Page