Global hotkey problem in Leopard

  • Dear list,

    Please forgive me if this should have been posted on carbon-dev, but I
    believe it has more to do with the cocoa implementation than carbon
    itself (and there is no Cocoa way of doing it), so here goes:

    In Tiger, I used the (stripped down) code below to enable/disable
    global hotkeys. In Leopard, the code no longer works. The keys are
    registered and unregistered alright, but the HotKeyEventHandlerProc
    function is never called. At first, I thought something was broken in
    Leopard, but I verified the Apple sample code that demonstrates the
    use of global hotkeys (the iTunes controller) still works as expected,
    so I now believe the problem lies elsewhere. Please help me out!
    Thanks.

    #import "Hotkey.h"

    void    RegisterHotKey( Boolean registerKeys );
    const    EventTypeSpec    hotKeyEvents[]        = { { kEventClassKeyboard,
    kEventHotKeyPressed }, { kEventClassKeyboard, kEventHotKeyReleased
    }    };
    static    OSStatus    HotKeyEventHandlerProc( EventHandlerCallRef inCallRef,
    EventRef inEvent, void* inUserData );

    @implementation Hotkey

    - (void)awakeFromNib
    {
    InstallApplicationEventHandler(
    NewEventHandlerUPP(HotKeyEventHandlerProc),
    GetEventTypeCount(hotKeyEvents), hotKeyEvents, 0, NULL );

    RegisterHotKey( true );

    }

    void    RegisterHotKey( Boolean registerKeys )
    {

    static Boolean    hotKeysRegistered;
    static EventHotKeyRef  eventHotKeyRef;

    if ( registerKeys == true )
    {
      if ( hotKeysRegistered == false )
      {

      EventHotKeyID  quitID  = { 'XHfq', 1 };
      OSStatus err = RegisterEventHotKey( 12, (cmdKey | optionKey),
    quitID, GetApplicationEventTarget(), 0, &eventHotKeyRef );

      if (err)  NSLog(@"Error installing event handler: %d",err);

      hotKeysRegistered = true;
            }
    }
    else if ( registerKeys == false )
    {
            if ( hotKeysRegistered == true )
            {
      UnregisterEventHotKey( eventHotKeyRef );

      hotKeysRegistered = false;
            }
    }

    return;
    }

    static    OSStatus    HotKeyEventHandlerProc( EventHandlerCallRef inCallRef,
    EventRef inEvent, void* inUserData )
    {
    NSLog(@"OK");

    }
  • On Nov 11, 2007, at 3:52 AM, <slasktrattenator...> wrote:

    > Dear list,
    >
    > Please forgive me if this should have been posted on carbon-dev, but I
    > believe it has more to do with the cocoa implementation than carbon
    > itself (and there is no Cocoa way of doing it), so here goes:

    Your code looks OK to me, so I think you should file a bug. Please
    include a test app that demonstrates the problem and let me know the
    bug number.

    -eric
  • Thanks Eric. I rolled up a sample project to demonstrate the problem,
    and guess what - it worked. So I opened up my real project and - it
    works again. I swear to God I did not touch that project since last
    night, when I spent hours on trying to figure out the problem. And
    today everything is back to normal. Strange, but what a relief! Thanks
    again and sorry for wasting your time.

    On Nov 11, 2007 5:28 PM, Eric Schlegel <ericsc...> wrote:
    >
    > On Nov 11, 2007, at 3:52 AM, <slasktrattenator...> wrote:
    >
    >> Dear list,
    >>
    >> Please forgive me if this should have been posted on carbon-dev, but I
    >> believe it has more to do with the cocoa implementation than carbon
    >> itself (and there is no Cocoa way of doing it), so here goes:
    >
    > Your code looks OK to me, so I think you should file a bug. Please
    > include a test app that demonstrates the problem and let me know the
    > bug number.
    >
    > -eric
    >
    >
previous month november 2007 next month
MTWTFSS
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    
Go to today