PDA

View Full Version : Calling a Cocoa method from Carbon code




AshleyByrom
Sep 5, 2010, 05:23 AM
Hi,

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;
GetEventParameter(theEvent,kEventParamDirectObject,typeEventHotKeyID,NULL,sizeof(hkCom),NULL,&hkCom);
int l = hkCom.id;

switch (l) {
case 1:
NSLog(@"Decrease shorcut!");
break;
case 2:
NSLog(@"Increase shortcut!");
break;
}
[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?

Thanks,
Ash



lee1210
Sep 5, 2010, 07:58 AM
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.

-Lee

AshleyByrom
Sep 5, 2010, 11:32 AM
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.

-Lee

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?

lee1210
Sep 5, 2010, 11:41 AM
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.

-Lee

AshleyByrom
Sep 5, 2010, 01:06 PM
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.

-Lee

http://cocoakids.net/global-hotkeys-in-cocoa-on-snow-leopard

^^ 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?

robbieduncan
Sep 5, 2010, 01:27 PM
Cabon is supported in SL. Carbon GUIs cannot use 64bit mode (whereas Cocoa ones can in SL).

AshleyByrom
Sep 5, 2010, 01:49 PM
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!

aliasunknown123
Nov 7, 2010, 12:21 PM
Please could post your code on how you did this