Professional Web Applications Themes

hotkey example? - Mac Programming

Could somebody please give me a full example of setting up a hotkey in a cocoa application? In my searching, I've learned that Cocoa doesn't have support for hotkeys, so you need to use the carbon API. Further, I even know which function it is (RegisterEventHotKey). The nice little how-to at http://www.unsanity.org/archives/000045.php has told me what I need to get the app to handle the hotkey (I assume). But, alas, I'm not sure exactly what I need to pass into RegisterEventHotKey in order to get it to work. Does anybody have a working example to share? And yes, I know ...

  1. #1

    Default hotkey example?

    Could somebody please give me a full example of setting up a hotkey in a
    cocoa application?

    In my searching, I've learned that Cocoa doesn't have support for
    hotkeys, so you need to use the carbon API. Further, I even know which
    function it is (RegisterEventHotKey). The nice little how-to at
    http://www.unsanity.org/archives/000045.php has told me what I need to
    get the app to handle the hotkey (I assume).

    But, alas, I'm not sure exactly what I need to pass into
    RegisterEventHotKey in order to get it to work. Does anybody have a
    working example to share?

    And yes, I know things like my inTarget needs to be an EventTargetRef,
    but without knowing actual values to pass in, it doesn't help much.

    Thanks,

    -Jim.....
    Jim Guest

  2. #2

    Default Re: hotkey example?

    On Fri, 2 Apr 2004, Jim Thomason wrote:
     

    Assuming you don't need to support 10.1 or lower, it's very easy; the
    standard Carbon stuff works just like it does in Carbon. You have to do
    two things. One, you must register a hotkey event handler:

    EventHandlerUPP myUPP = NewEventHandlerUPP(HotKeyHandler);
    OSStatus theErr;
    event.eventClass = kEventClassKeyboard;
    event.eventKind = kEventHotKeyPressed;
    theErr = InstallApplicationEventHandler(myUPP, 1, &event, self, NULL);

    Where HotKeyHandler() is the function that will be called when the hotkey
    is pressed. I pass self as the userdata so that I can call back to the
    object where this code is, but you can use your own sort of technique from
    here.

    The other thing is to register the hot keys themselves. Note that you do
    the above only one time, and then you do this as many times as you want;
    you can register multiple hot keys, or register one and register another
    one later, etc.

    EventHotKeyID keyID = {0, 0};
    theErr = RegisterEventHotKey(48, hotKey, keyID,
    GetApplicationEventTarget(), 0, &hotKeyRef);

    The keyID is passed to your handler, so you should use separate ones if
    you want to register multiple hotkeys and differentiate between them. The
    hotKeyRef is of type EventHotKeyRef, and you need to hang on to it if you
    ever want to unregister the hotkey. The hotKey variable should be
    something like cmdKey, optionKey, etc., or more than one or'd together.
    Note that you *must* provide at least one modifier key; this API does not
    allow you to register hotkeys that don't include a modifier.
    Michael Guest

  3. #3

    Default Re: hotkey example?

    In article <twistedsys.net>,
    Michael Ash <com> wrote:
     
    >
    > Assuming you don't need to support 10.1 or lower, it's very easy; the
    > standard Carbon stuff works just like it does in Carbon. You have to do
    > two things. One, you must register a hotkey event handler:
    >
    > EventHandlerUPP myUPP = NewEventHandlerUPP(HotKeyHandler);
    > OSStatus theErr;
    > event.eventClass = kEventClassKeyboard;
    > event.eventKind = kEventHotKeyPressed;
    > theErr = InstallApplicationEventHandler(myUPP, 1, &event, self, NULL);[/ref]

    The UPP junk is not needed nowadays. You can just write this as

    InstallApplicationEventHandler(& HotKeyHandler, 1, &event, self, NULL);

    hth

    meeroh

    --
    If this message helped you, consider buying an item
    from my wish list: <http://web.meeroh.org/wishlist>

    Miro Guest

Similar Threads

  1. how to create hotkey with flex
    By zfleet in forum Macromedia Flex General Discussion
    Replies: 0
    Last Post: April 14th, 09:48 AM
  2. hotkey problems..
    By Chris_ webforumsuser@macromedia.com in forum Macromedia Fireworks
    Replies: 2
    Last Post: August 25th, 10:47 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139