Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

AshleyByrom

macrumors newbie
Original poster
Jan 24, 2010
6
0
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

Code:
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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

macrumors newbie
Original poster
Jan 24, 2010
6
0
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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

macrumors newbie
Original poster
Jan 24, 2010
6
0
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?
 

AshleyByrom

macrumors newbie
Original poster
Jan 24, 2010
6
0
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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.