views:

62

answers:

3

Hi All,

I'm writing a 64-bit Cocoa application. I need to register for global key events. So I wrote this piece of code :

- (void)awakeFromNib
{
    EventHotKeyRef gMyHotKeyRef;
    EventHotKeyID gMyHotKeyID;
    EventTypeSpec eventType;
    eventType.eventClass=kEventClassKeyboard;
    eventType.eventKind=kEventHotKeyPressed;
    eventType.eventClass=kEventClassKeyboard;
    eventType.eventKind=kEventHotKeyPressed;
    InstallApplicationEventHandler(&MyHotKeyHandler,1,&eventType,NULL,NULL);
    gMyHotKeyID.signature='htk1';
    gMyHotKeyID.id=1;
    RegisterEventHotKey(49, cmdKey+optionKey, gMyHotKeyID,
     **GetApplicationEventTarget**(), 0, &gMyHotKeyRef);
}

But since GetApplicationEventTarget() is not supported for 64-bit applications I'm getting errors. If I declare it, then I don't get any errors but the application crashes.

Is there any equivalent method for GetApplicationEventTarget() (defined in Carbon framework) to use in 64-bit applications.

Or is there any way to get the global key events using cocoa calls?

Any help is appreciated.

Thanks, Dheeraj.

A: 

Carbon isn't supported in 64-bit applications. See the answer to this question for information on how to use CGEventTap to do this in a supported way in Cocoa.

Alex
This is not entirely true. Some of Carbon was upgraded to 64-bit goodness. However, the carbon UI stuff, for example, was not.
Dave DeLong
Thanks a lot for replying Alex.I tried using CGEventTap as explained in the link above. But the application crashes if I register for CGEventMaskBit(kCGEventKeyUp) or CGEventMaskBit(kCGEventKeyDown), but works fine if I use CGEventMaskBit(kCGEventFlagsChanged).The application crashes in this line:CFRunLoopSourceRef keyUpRunLoopSourceRef = CFMachPortCreateRunLoopSource(NULL, keyUpEventTap, 0);what could be the issue?
Dheeraj
A: 

I wrote a Cocoa wrapper for Carbon hot keys (and as far as my testing showed, it works in 64-bit apps), and you can find it on github here: http://github.com/davedelong/DDHotKey

I'm using GetEventDispatcherTarget() for hotkey registration.

Dave DeLong
Thanks a lot for replying Dave.I tried using GetEventDispatcherTarget(). The code compiles fine also when I press the hot key, control comes to the registered function but after that the application crashes. This is the callstack : (some kind of recursive calls)#0 0x8009eba9 in DispatchEventToHandlers#1 0x8009e4bf in SendEventToEventTargetInternal#2 0x8009e339 in SendEventToEventTargetWithOptions#3 0x800c0582 in ToolboxEventDispatcherHandler#4 0x8009f38e in DispatchEventToHandlers#5 0x8009e4bf in SendEventToEventTargetInternal....... (and it goes on.)what could be wrong?
Dheeraj
A: 

I think it is a documentation error when it says that GetApplicationEventTarget is not supported in 64 bits. If you look in CarbonEvents.h (from the 10.6 SDK), you see that the declaration of GetUserFocusEventTarget is bracketed by #if !__LP64__ ... #endif, but just above it, the declaration of GetApplicationEventTarget is not. GetApplicationEventTarget is probably not the cause of the crash. In your code, gMyHotKeyRef and gMyHotKeyID look like they were intended to be global variables, but they're local.

JWWalker
But for me GetApplicationEventTarget() is inside #if !__LP64__ ... #endif block.
Dheeraj
That's odd. Which SDK? Which version of Xcode? (Mind is Xcode 3.2.2, and I checked both 10.5 and 10.6 SDKs.)
JWWalker
/* * GetApplicationEventTarget() * * Discussion: * Returns the EventTargetRef for the application. Once you obtain * * Availability: * Mac OS X: in version 10.0 and later in Carbon.framework [32-bit only] * Non-Carbon CFM: not available */I even checked the documentation. It's mentioned that the call is supported only for 32-bit applications.XCode Version : 3.1.2SDK : 10.5Thanks for the reply. :)
Dheeraj