aboutsummaryrefslogtreecommitdiff
path: root/AppKit/GTMCarbonEvent.m
blob: 3f512718e79f6b05c12b06f7b1915371f1c375cc (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
//
//  GTMCarbonEvent.m
//
//  Copyright 2006-2008 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.
//

#import "GTMCarbonEvent.h"
#import <AppKit/AppKit.h>
#import "GTMDebugSelectorValidation.h"
#import "GTMTypeCasting.h"

// Wrapper for all the info we need about a hotkey that we can store in a
// Foundation storage class.
@interface GTMCarbonHotKey (GTMCarbonHotKeyPrivate)

// Create a HotKey record
//  Arguments:
//    keyID - id of the hotkey
//    target - object we are going to call when the hotkey is hit
//    action - selector we are going to call on target
//    userInfo - user storage
//    whenPressed - do we do it on key down or key up?
//  Returns:
//    a hotkey record, or nil on failure
- (id)initWithHotKey:(EventHotKeyID)keyID
              target:(id)target
              action:(SEL)selector
            userInfo:(id)userInfo
         whenPressed:(BOOL)onKeyDown;

// Does this record match key |keyID|
//  Arguments:
//    keyID - the id to match against
//  Returns:
//    Yes if we match this key id
- (BOOL)matchesHotKeyID:(EventHotKeyID)keyID;

// Make target perform selector
//  Returns:
//    Yes if handled
- (BOOL)sendAction;

- (void)setHotKeyRef:(EventHotKeyRef)ref;
@end

@implementation GTMCarbonEvent

//  Create an event of class |inClass| and kind |inKind|
//
//  Returns:
//    Autoreleased GTMCarbonEvent
//
+ (id)eventWithClass:(UInt32)inClass kind:(UInt32)kind {
  return [[[self alloc] initWithClass:inClass kind:kind] autorelease];
}


//  Create an event based on |event|. Retains |event|.
//
//  Returns:
//    Autoreleased GTMCarbonEvent
//
+ (id)eventWithEvent:(EventRef)event {
  return [[[self alloc] initWithEvent:event] autorelease];
}


//  Create an event based on the event currently being handled.
//
//  Returns:
//    Autoreleased GTMCarbonEvent
//
+ (id)currentEvent {
  return [self eventWithEvent:GetCurrentEvent()];
}


//  Create an event of class |inClass| and kind |inKind|
//
//  Returns:
//    GTMCarbonEvent
//
- (id)initWithClass:(UInt32)inClass kind:(UInt32)kind {
  if ((self = [super init])) {
    __Verify_noErr(CreateEvent(kCFAllocatorDefault, inClass, kind,
                               0, kEventAttributeNone, &event_));
  }
  return self;
}


//  Create an event based on |event|. Retains |event|.
//
//  Returns:
//    GTMCarbonEvent
//
- (id)initWithEvent:(EventRef)event {
  if ((self = [super init])) {
    if (event) {
      event_ = RetainEvent(event);
    }
  }
  return self;
}


// This does a proper event copy, but ignores the |zone|. No way to do a copy
// of an event into a specific zone.
//
//  Arguments:
//    zone - the zone to copy to
//  Returns:
//    the copied event. nil on failure
- (id)copyWithZone:(NSZone *)zone {
  GTMCarbonEvent *carbonEvent = nil;
  EventRef newEvent = CopyEvent([self event]);
  if (newEvent) {
    carbonEvent = [[[self class] allocWithZone:zone] initWithEvent:newEvent];
    ReleaseEvent(newEvent);
  }
  return carbonEvent;
}

// releases our retained event
//
- (void)dealloc {
  if (event_) {
    ReleaseEvent(event_);
    event_ = NULL;
  }
  [super dealloc];
}

// description utliity for debugging
//
- (NSString *)description {
  // Use 8 bytes because stack protection gives us a warning if we use a
  // smaller buffer.
  char cls[8];
  UInt32 kind;

  // Need everything bigendian if we are printing out the class as a "string"
  *((UInt32 *)cls) = CFSwapInt32HostToBig([self eventClass]);
  kind = [self eventKind];
  cls[4] = 0;
  return [NSString stringWithFormat:@"GTMCarbonEvent '%s' %lu",
          cls, (unsigned long)kind];
}


//  Get the event's class.
//
//  Returns:
//    event class
//
- (UInt32)eventClass {
  return GetEventClass(event_);
}


//  Get the event's kind.
//
//  Returns:
//    event kind
//
- (UInt32)eventKind {
  return GetEventKind(event_);
}


//  Set the event's time.
//
//  Arguments:
//    time - the time you want associated with the event
//
- (void)setTime:(EventTime)eventTime {
  __Verify_noErr(SetEventTime(event_, eventTime));
}


//  Get the event's time.
//
//  Returns:
//    the time associated with the event
//
- (EventTime)time {
  return GetEventTime(event_);
}


//  Get the event's eventref for passing to other carbon functions.
//
//  Returns:
//    the event ref associated with the event
//
- (EventRef)event {
  return event_;
}


//  Sends event to an event target with options
//
//  Arguments:
//    target - target to send event to.
//    options - options to send event. See SendEventToEventTargetWithOptions
//              for details.
//
//  Returns:
//    OSStatus value.
//
- (OSStatus)sendToTarget:(GTMCarbonEventHandler *)target
                 options:(OptionBits)options {
  return SendEventToEventTargetWithOptions(event_,
                                           [target eventTarget], options);
}

//  Post event to an event queue.
//
//  Arguments:
//    queue - queue to post it to.
//    priority - priority to post it with
//
//  Returns:
//    OSStatus value.
//
- (OSStatus)postToQueue:(EventQueueRef)queue priority:(EventPriority)priority {
  return PostEventToQueue(queue, event_, priority);
}


//  Post event to current queue with standard priority.
//
- (void)postToCurrentQueue {
  __Verify_noErr([self postToQueue:GetCurrentEventQueue()
                          priority:kEventPriorityStandard]);
}


//  Post event to main queue with standard priority.
//
- (void)postToMainQueue {
  __Verify_noErr([self postToQueue:GetMainEventQueue()
                          priority:kEventPriorityStandard]);
}


//  Sets (or adds) a parameter to an event. Try not to use this function
//  directly. Look at the PARAM_TEMPLATE_DECL/DEFN macros below.
//
//  Arguments:
//    name - the parameter name.
//    type - the parameter type.
//    size - the size of the data that |data| points to.
//    data - pointer to the data you want to set the parameter to.
//
- (void)setParameterNamed:(EventParamName)name
                     type:(EventParamType)type
                     size:(ByteCount)size
                     data:(const void *)data {
  __Verify_noErr(SetEventParameter(event_, name, type, size, data));
}


//  Gets a parameter from an event. Try not to use this function
//  directly. Look at the PARAM_TEMPLATE_DECL/DEFN macros below.
//
//  Arguments:
//    name - the parameter name.
//    type - the parameter type.
//    size - the size of the data that |data| points to.
//    data - pointer to the buffer that you want to fill with your data.
//
//  Returns:
//    YES is parameter is retrieved successfully. NO if parameter doesn't exist.
//
- (BOOL)getParameterNamed:(EventParamName)name
                     type:(EventParamType)type
                     size:(ByteCount)size
                     data:(void *)data {
  OSStatus status = GetEventParameter(event_, name, type,
                                      NULL, size, NULL, data);
  return status == noErr;
}


//  Gets a the size of a parameter from an event.
//
//  Arguments:
//    name - the parameter name.
//    type - the parameter type.
//
//  Returns:
//    The size of the buffer required to hold the parameter. 0 if parameter
//    doesn't exist.
//
- (ByteCount)sizeOfParameterNamed:(EventParamName)name
                             type:(EventParamType)type {
  ByteCount size = 0;
  __Verify_noErr(GetEventParameter(event_, name, type, NULL, 0, &size, NULL));
  return size;
}

@end

@implementation GTMCarbonEvent (GTMCarbonEventGettersAndSetters)
GTM_PARAM_TEMPLATE_DEFN(UInt32)
GTM_PARAM_TEMPLATE_DEFN(EventHotKeyID)
@end

UInt32 GTMCocoaToCarbonKeyModifiers(NSUInteger inCocoaModifiers) {
  UInt32 carbModifiers = 0;
  if (inCocoaModifiers & NSAlphaShiftKeyMask) carbModifiers |= alphaLock;
  if (inCocoaModifiers & NSShiftKeyMask) carbModifiers |= shiftKey;
  if (inCocoaModifiers & NSControlKeyMask) carbModifiers |= controlKey;
  if (inCocoaModifiers & NSAlternateKeyMask) carbModifiers |= optionKey;
  if (inCocoaModifiers & NSCommandKeyMask) carbModifiers |= cmdKey;
  return carbModifiers;
}

NSUInteger GTMCarbonToCocoaKeyModifiers(UInt32 inCarbonModifiers) {
  NSUInteger nsModifiers = 0;
  if (inCarbonModifiers & alphaLock) nsModifiers |= NSAlphaShiftKeyMask;
  if (inCarbonModifiers & shiftKey) nsModifiers |= NSShiftKeyMask;
  if (inCarbonModifiers & controlKey) nsModifiers |= NSControlKeyMask;
  if (inCarbonModifiers & optionKey) nsModifiers |= NSAlternateKeyMask;
  if (inCarbonModifiers & cmdKey) nsModifiers |= NSCommandKeyMask;
  return nsModifiers;
}

const OSType kGTMCarbonFrameworkSignature = 'GTM ';

@implementation GTMCarbonEventHandler

-(void)dealloc {
  if (eventHandler_) {
    __Verify_noErr(RemoveEventHandler(eventHandler_));
  }
  [super dealloc];
}
// Does our delegate respond to eventHandler:receivedEvent:handler:
//
// Returns:
//  YES if delegate responds to eventHandler:receivedEvent:handler:
- (BOOL) delegateRespondsToHandleEvent {
  return delegateRespondsToHandleEvent_;
}

// Registers the event handler to listen for |events|.
//
// Arguments:
//   events - an array of EventTypeSpec. The events to register for.
//   count - the number of EventTypeSpecs in events.
//
- (void)registerForEvents:(const EventTypeSpec *)events count:(size_t)count {
  __Verify_noErr(AddEventTypesToHandler([self eventHandler], count, events));
}

// Causes the event handler to stop listening for |events|.
//
// Arguments:
//   events - an array of EventTypeSpec. The events to register for.
//   count - the number of EventTypeSpecs in events.
//
- (void)unregisterForEvents:(const EventTypeSpec *)events count:(size_t)count {
  __Verify_noErr(
      RemoveEventTypesFromHandler([self eventHandler], count, events));
}

// To be overridden by subclasses to respond to events. All subclasses should
// call [super handleEvent:handler:] if they don't handle the event themselves.
//
// Arguments:
//   event - the event to be handled
//   handler - the call ref in case you want to call CallNextEventHandler
//             in your method
// Returns:
//   OSStatus - usually either noErr or eventNotHandledErr
//
- (OSStatus)handleEvent:(GTMCarbonEvent *)event
                handler:(EventHandlerCallRef)handler {
  OSStatus status = eventNotHandledErr;
  __Require(event, CantUseParams);
  __Require(handler, CantUseParams);
  __Require([event event], CantUseParams);
  status = CallNextEventHandler(handler, [event event]);
CantUseParams:
  return status;
}

// To be overridden by subclasses to return the event target for the class.
// GTMCarbonEventHandler's implementation returns NULL.
//
// Returns:
//   The event target ref.
//
- (EventTargetRef)eventTarget {
  // Defaults implementation needs to be overridden
  return NULL;
}

// C callback for our registered EventHandlerUPP.
//
// Arguments:
//  inHandler - handler given to us from Carbon Event system
//  inEvent - the event we are handling
//  inUserData - refcon that we gave to the carbon event system. Is a
//               GTMCarbonEventHandler in disguise.
// Returns:
//  status of event handler
//
static OSStatus EventHandler(EventHandlerCallRef inHandler,
                             EventRef inEvent,
                             void *inUserData) {
  GTMCarbonEvent *event = [GTMCarbonEvent eventWithEvent:inEvent];
  GTMCarbonEventHandler *handler
    = GTM_STATIC_CAST(GTMCarbonEventHandler, inUserData);

  // First check to see if our delegate cares about this event. If the delegate
  // handles it (i.e responds to it and does not return eventNotHandledErr) we
  // do not pass it on to default handling.
  OSStatus status = eventNotHandledErr;
  if ([handler delegateRespondsToHandleEvent]) {
    status = [[handler delegate] gtm_eventHandler:handler
                                    receivedEvent:event
                                          handler:inHandler];
  }
  if (status == eventNotHandledErr) {
    status = [handler handleEvent:event handler:inHandler];
  }
  return status;
}

// Gets the underlying EventHandlerRef for that this class wraps.
//
// Returns:
//   The EventHandlerRef this class wraps.
//
- (EventHandlerRef)eventHandler {
  if (!eventHandler_) {
    static EventHandlerUPP sHandlerProc = NULL;
    if ( sHandlerProc == NULL ) {
      sHandlerProc = NewEventHandlerUPP(EventHandler);
    }
    __Verify_noErr(InstallEventHandler([self eventTarget],
                                       sHandlerProc, 0,
                                       NULL, self, &eventHandler_));
  }
  return eventHandler_;
}

// Gets the delegate for the handler
//
// Returns:
//   the delegate
- (id)delegate {
  return delegate_;
}

// Sets the delegate for the handler and caches whether it responds to
// the eventHandler:receivedEvent:handler: selector for performance purposes.
//
// Arguments:
//   delegate - the delegate for the handler
- (void)setDelegate:(id)delegate {
  delegate_ = delegate;
  SEL selector = @selector(gtm_eventHandler:receivedEvent:handler:);
  delegateRespondsToHandleEvent_ = [delegate respondsToSelector:selector];
}

@end

@implementation GTMCarbonEventMonitorHandler

+ (GTMCarbonEventMonitorHandler *)sharedEventMonitorHandler {
  static GTMCarbonEventMonitorHandler *obj = nil;
  if (!obj) {
    obj = [[self alloc] init];
  }
  return obj;
}

- (EventTargetRef)eventTarget {
  return GetEventMonitorTarget();
}

@end

#if (MAC_OS_X_VERSION_MAX_ALLOWED == MAC_OS_X_VERSION_10_5)
// Accidentally marked as !LP64 in the 10.5sdk, it's back in the 10.6 sdk.
// If you remove this decl, please remove it from GTMCarbonEventTest.m as well.
extern EventTargetRef GetApplicationEventTarget(void);
#endif  // (MAC_OS_X_VERSION_MAX_ALLOWED == MAC_OS_X_VERSION_10_5)

@implementation GTMCarbonEventApplicationEventHandler

+ (GTMCarbonEventApplicationEventHandler *)sharedApplicationEventHandler {
  static GTMCarbonEventApplicationEventHandler *obj = nil;
  if (!obj) {
    obj = [[self alloc] init];
  }
  return obj;
}

- (EventTargetRef)eventTarget {
  return GetApplicationEventTarget();
}

@end

@implementation GTMCarbonEventDispatcherHandler

+ (GTMCarbonEventDispatcherHandler *)sharedEventDispatcherHandler {
  static GTMCarbonEventDispatcherHandler *obj = nil;
  if (!obj) {
    obj = [[self alloc] init];
  }
  return obj;
}

// Register for the events we handle, and set up the dictionaries we need
// to keep track of the hotkeys and commands that we handle.
//  Returns:
//    GTMCarbonApplication or nil on failure
- (id)init {
  if ((self = [super init])) {
    static EventTypeSpec events[] = {
      { kEventClassKeyboard, kEventHotKeyPressed },
      { kEventClassKeyboard, kEventHotKeyReleased },
    };
    [self registerForEvents:events count:GetEventTypeCount(events)];
    hotkeys_ = [[NSMutableArray alloc] initWithCapacity:0];
  }
  return self;
}

// COV_NF_START
// Singleton, we never get released. Just here for completeness.
- (void)dealloc {
  [hotkeys_ release];
  [super dealloc];
}
// COV_NF_END

- (EventTargetRef)eventTarget {
  return GetEventDispatcherTarget();
}

// Registers a hotkey. When the hotkey is executed by the user, target will be
// called with selector.
//  Arguments:
//    keyCode - the virtual keycode of the hotkey
//    cocoaModifiers - the modifiers that need to be used with |keyCode|. NB
//                     that these are cocoa modifiers, so NSCommandKeyMask etc.
//    target - instance that will get |action| called when the hotkey fires
//    action - the method to call on |target| when the hotkey fires
//             action should have the signature - (void)handler:(GTMCarbonEventDispatcherHandler *)handler
//    userInfo - user storage
//    onKeyDown - is YES, the hotkey fires on the keydown (usual) otherwise
//              it fires on the key up.
//  Returns:
//    a EventHotKeyRef that you can use with other Carbon functions, or for
//    unregistering the hotkey. Note that all hotkeys are unregistered
//    automatically when an app quits. Will be NULL on failure.
- (GTMCarbonHotKey *)registerHotKey:(NSUInteger)keyCode
                          modifiers:(NSUInteger)cocoaModifiers
                             target:(id)target
                             action:(SEL)selector
                           userInfo:(id)userInfo
                        whenPressed:(BOOL)onKeyDown {
  static UInt32 sCurrentID = 0;

  GTMCarbonHotKey *newKey = nil;
  EventHotKeyRef theRef = NULL;
  EventHotKeyID keyID;
  keyID.signature = kGTMCarbonFrameworkSignature;
  keyID.id = ++sCurrentID;
  newKey = [[[GTMCarbonHotKey alloc] initWithHotKey:keyID
                                             target:target
                                             action:selector
                                           userInfo:userInfo
                                        whenPressed:onKeyDown] autorelease];
  __Require(newKey, CantCreateKey);
  __Require_noErr_Action(RegisterEventHotKey((UInt32)keyCode,
                                             GTMCocoaToCarbonKeyModifiers(cocoaModifiers),
                                             keyID,
                                             [self eventTarget],
                                             0,
                                             &theRef),
                         CantRegisterHotKey, newKey = nil);
  [newKey setHotKeyRef:theRef];
  [hotkeys_ addObject:newKey];

CantRegisterHotKey:
CantCreateKey:
  return newKey;
}

// Unregisters a hotkey previously registered with registerHotKey.
//  Arguments:
//    keyRef - the EventHotKeyRef to unregister
- (void)unregisterHotKey:(GTMCarbonHotKey *)keyRef {
  __Check([hotkeys_ containsObject:keyRef]);
  [[keyRef retain] autorelease];
  [hotkeys_ removeObject:keyRef];
  __Verify_noErr(UnregisterEventHotKey([keyRef hotKeyRef]));
}

// A hotkey has been hit. See if it is one of ours, and if so fire it.
//  Arguments:
//    event - the hotkey even that was received
//  Returns:
//    Yes if handled.
- (BOOL)handleHotKeyEvent:(GTMCarbonEvent *)event {
  EventHotKeyID keyID;
  BOOL handled = [event getEventHotKeyIDParameterNamed:kEventParamDirectObject
                                                  data:&keyID];
  if (handled) {
    GTMCarbonHotKey *hotkey;
    for (hotkey in hotkeys_) {
      if ([hotkey matchesHotKeyID:keyID]) {
        EventKind kind = [event eventKind];
        BOOL onKeyDown = [hotkey onKeyDown];
        if ((kind == kEventHotKeyPressed && onKeyDown) ||
            (kind == kEventHotKeyReleased && !onKeyDown)) {
          handled = [hotkey sendAction];
        }
        break;
      }
    }
  }
  return handled;
}

// Currently we handle hotkey and command events here. If we get one of them
// we dispatch them off to the handlers above. Otherwise we just call up to
// super.
//  Arguments:
//    event - the event to check
//    handler - the handler call ref
//  Returns:
//    OSStatus
- (OSStatus)handleEvent:(GTMCarbonEvent *)event
                handler:(EventHandlerCallRef)handler {
  OSStatus theStatus = eventNotHandledErr;
  if ([event eventClass] == kEventClassKeyboard) {
    EventKind kind = [event eventKind];
    if (kind == kEventHotKeyPressed || kind == kEventHotKeyReleased) {
      theStatus = [self handleHotKeyEvent:event] ? noErr : eventNotHandledErr;
    }
  }
  // We didn't handle it, maybe somebody upstairs will.
  if (theStatus == eventNotHandledErr) {
    theStatus = [super handleEvent:event handler:handler];
  }
  return theStatus;
}

@end

@implementation GTMCarbonHotKey

// Init a HotKey record. In debug version make sure that the selector we are
// passed matches what we expect. (
//  Arguments:
//    keyID - id of the hotkey
//    reference - hotkey reference
//    target - object we are going to call when the hotkey is hit
//    action - selector we are going to call on target
//    userinfo - info for user
//    whenPressed - do we do it on key down or key up?
//  Returns:
//    a hotkey record, or nil on failure
- (id)initWithHotKey:(EventHotKeyID)keyID
              target:(id)target
              action:(SEL)selector
            userInfo:(id)userInfo
         whenPressed:(BOOL)onKeyDown {
  if ((self = [super init])) {
    if(!target || !selector) {
      [self release];
      return nil;
    }
    id_ = keyID;
    target_ = [target retain];
    userInfo_ = [userInfo retain];
    selector_ = selector;
    onKeyDown_ = onKeyDown;
    GTMAssertSelectorNilOrImplementedWithReturnTypeAndArguments(target,
                                                                selector,
                                                                @encode(void),
                                                                @encode(id),
                                                                NULL);
  }
  return self;
}

- (void)dealloc {
  [target_ release];
  [userInfo_ release];
  [super dealloc];
}

- (NSUInteger)hash {
  return (NSUInteger)hotKeyRef_;
}

- (BOOL)isEqual:(id)object {
  return [object isMemberOfClass:[self class]]
    && (hotKeyRef_ == [object hotKeyRef]);
}

// Does this record match key |keyID|
//  Arguments:
//    keyID - the id to match against
//  Returns:
//    Yes if we match this key id
- (BOOL)matchesHotKeyID:(EventHotKeyID)keyID {
  return (id_.signature == keyID.signature) && (id_.id == keyID.id);
}

- (BOOL)sendAction {
  BOOL handled = NO;
  @try {
    [target_ performSelector:selector_ withObject:self];
    handled = YES;
  }
  @catch (NSException * e) {
    handled = NO;
    _GTMDevLog(@"Exception fired in hotkey: %@ (%@)", [e name], [e reason]);
  }  // COV_NF_LINE
  return handled;
}

- (BOOL)onKeyDown {
  return onKeyDown_;
}

- (id)userInfo {
  return userInfo_;
}

- (EventHotKeyRef)hotKeyRef {
  return hotKeyRef_;
}

- (void)setHotKeyRef:(EventHotKeyRef)ref {
  hotKeyRef_ = ref;
}

- (NSString *)description {
  return [NSString stringWithFormat:@"<%@ %p> - ref %p signature %lu id %lu",
          [self class], self, hotKeyRef_,
          (unsigned long)id_.signature, (unsigned long)id_.id];
}

@end