[Q] capture or assign global hot key like spotlight or quicksilver

  • Hi,

    I'd like to create application like quicksilver or sptlight. They
    appear to the screen when user presses specified hot key
    and the hot key is global - they work even if your active
    application is not them.

    Are there any sample application or resource/documentation
    for this?

    Thanks in advance.
  • Am 15.11.2007 um 15:26 schrieb S.J.Chun:
    > I'd like to create application like quicksilver or sptlight. They
    > appear to the screen when user presses specified hot key
    > and the hot key is global - they work even if your active
    > application is not them.
    >
    > Are there any sample application or resource/documentation
    > for this?

      You'll have to use Carbon's RegisterEventHotKey() for that. Google
    for "cocoa and hotkey", there's a lot of sample code out there that
    wraps it in a nice Cocoa class.

    Cheers,
    -- M. Uli Kusterer
    "The Witnesses of TeachText are everywhere..."
    http://www.zathras.de
  • Have a look at Apple's iTunesController sample code, if you want
    several keys. If you just want one single hotkey, here's a simple
    implementation:

    #import "Hotkey.h" // Import Carbon.h in the header

    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 )
      {

                            // Set name of your key
      EventHotKeyID  keyID  = { 'hotK', 1 };

                          // Set keycode and modifiers for you key.
    Keycode 49 = space bar.
                          OSStatus err = RegisterEventHotKey( 49,
    cmdKey+optionKey, quitID,              GetApplicationEventTarget(),
    0, &eventHotKeyRef );

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

    return;
    }

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

    }

    On Nov 15, 2007 3:26 PM, "S.J.Chun" <chunsj...> wrote:
    > Hi,
    >
    > I'd like to create application like quicksilver or sptlight. They
    > appear to the screen when user presses specified hot key
    > and the hot key is global - they work even if your active
    > application is not them.
    >
    > Are there any sample application or resource/documentation
    > for this?
    >
    > Thanks in advance.
    >
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