-
Notifications
You must be signed in to change notification settings - Fork 35
/
NDHotKeyEvent.m
executable file
·1150 lines (997 loc) · 31.3 KB
/
NDHotKeyEvent.m
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
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* NDHotKeyEvent.m
* NDHotKeyEvent
*
* Created by Nathan Day on Wed Feb 26 2003.
* Copyright (c) 2002 Nathan Day. All rights reserved.
*/
#import "NDHotKeyEvent.h"
@interface NDHotKeyEvent (Private)
+ (NSHashTable *)allHotKeyEvents;
- (BOOL)addHotKey;
- (void)removeHotKey;
- (BOOL)setCollectiveEnabled:(BOOL)aFlag;
- (BOOL)collectiveEnable;
@end
static NSString * kArchivingKeyCodeKey = @"KeyCodeKey",
* kArchivingCharacterKey = @"CharacterKey",
* kArchivingModifierFlagsKey = @"ModifierFlagsKey",
* kArchivingSelectorReleasedCodeKey = @"SelectorReleasedCodeKey",
* kArchivingSelectorPressedCodeKey = @"SelectorPressedCodeKey";
const OSType NDHotKeyDefaultSignature = 'NDHK';
static OSStatus switchHotKey( NDHotKeyEvent * self, BOOL aFlag );
/*
* class implementation NDHotKeyEvent
*/
@implementation NDHotKeyEvent
#ifdef NDHotKeyEventThreadSafe
static NSLock * hotKeysLock = NULL;
// ***warning Thread saftey has been enabled for NDHotKeyEvent class methods
#define NDHotKeyEventLock [hotKeysLock lock]
#define NDHotKeyEventUnlock [hotKeysLock unlock]
#else
// ***warning The NDHotKeyEvent class methods are NOT thread safe
#define NDHotKeyEventLock // lock
#define NDHotKeyEventUnlock // unlock
#endif
static NSHashTable * allHotKeyEvents = NULL;
static BOOL isInstalled = NO;
static OSType signature = 0;
unsigned int cocoaModifierFlagsToCarbonModifierFlags( unsigned int aModifierFlags );
pascal OSErr eventHandlerCallback( EventHandlerCallRef anInHandlerCallRef, EventRef anInEvent, void * self );
unsigned hashValueHashFunction( NSHashTable * aTable, const void * aHotKeyEvent );
BOOL isEqualHashFunction( NSHashTable * aTable, const void * aFirstHotKeyEvent, const void * aSecondHotKeyEvent);
NSString * describeHashFunction( NSHashTable * aTable, const void * aHotKeyEvent );
struct HotKeyMappingEntry
{
unsigned short keyCode;
unsigned int modifierFlags;
NDHotKeyEvent * hotKeyEvent;
};
/*
* +install
*/
+ (BOOL)install
{
if( isInstalled == NO )
{
NSHashTable * theHotKeyEvents = [self allHotKeyEvents];
EventTypeSpec theTypeSpec[] =
{
{ kEventClassKeyboard, kEventHotKeyPressed },
{ kEventClassKeyboard, kEventHotKeyReleased }
};
NDHotKeyEventLock;
if( theHotKeyEvents != nil && isInstalled == NO )
{
if( InstallEventHandler( GetEventDispatcherTarget(), NewEventHandlerUPP((EventHandlerProcPtr)eventHandlerCallback), 2, theTypeSpec, theHotKeyEvents, nil ) == noErr )
{
isInstalled = YES;
}
else
{
NSLog(@"Could not install Event handler");
}
}
NDHotKeyEventUnlock;
}
return isInstalled;
}
#ifdef NDHotKeyEventThreadSafe
/*
* +initialize:
*/
+ (void)initialize
{
while( hotKeysLock == nil )
{
NSLock * theInstance = [[NSLock alloc] init];
if( !CompareAndSwap( nil, (long int)theInstance, (long int*)&hotKeysLock) )
[theInstance release]; // did not use instance
}
}
#endif
/*
* +setSignature:
*/
+ (void)setSignature:(OSType)aSignature
{
NSAssert( signature == 0 || aSignature == signature, @"The signature used by NDHotKeyEvent can only be set once safely" );
signature = aSignature;
}
/*
* +signature
*/
+ (OSType)signature
{
signature = signature ? signature : NDHotKeyDefaultSignature;
return signature;
}
/*
* +setAllEnabled:
*/
+ (BOOL)setAllEnabled:(BOOL)aFlag
{
BOOL theAllSucceeded = YES;
NSHashTable * theHashTable = [NDHotKeyEvent allHotKeyEvents];
/*
* need to install before to make sure the method 'setCollectiveEnabled:'
* doesn't try install since install tries to aquire the lock 'hotKeysLock'
*/
if( theHashTable && [NDHotKeyEvent install] )
{
NSHashEnumerator theEnumerator;
struct HotKeyMappingEntry * theHotKeyMapEntry;
NDHotKeyEventLock;
theEnumerator = NSEnumerateHashTable( theHashTable );
while( (theHotKeyMapEntry = (struct HotKeyMappingEntry*)NSNextHashEnumeratorItem(&theEnumerator) ) )
{
if( ![theHotKeyMapEntry->hotKeyEvent setCollectiveEnabled:aFlag] )
theAllSucceeded = NO;
}
NSEndHashTableEnumeration( &theEnumerator );
NDHotKeyEventUnlock;
}
return theAllSucceeded;
}
/*
* +isEnabledKeyCode:modifierFlags:
*/
+ (BOOL)isEnabledKeyCode:(unsigned short)aKeyCode modifierFlags:(unsigned int)aModifierFlags
{
return [[self findHotKeyForKeyCode:aKeyCode modifierFlags:aModifierFlags] isEnabled];
}
/*
* +getHotKeyForKeyCode:character:modifierFlags:
*/
+ (NDHotKeyEvent *)getHotKeyForKeyCode:(unsigned short)aKeyCode character:(unichar)aChar modifierFlags:(unsigned int)aModifierFlags
{
NDHotKeyEvent * theHotKey = nil;
theHotKey = [self findHotKeyForKeyCode:aKeyCode modifierFlags:aModifierFlags];
return theHotKey ? theHotKey : [self hotKeyWithKeyCode:aKeyCode character:aChar modifierFlags:aModifierFlags];
}
/*
* +findHotKeyForKeyCode:modifierFlags:
*/
+ (NDHotKeyEvent *)findHotKeyForKeyCode:(unsigned short)aKeyCode modifierFlags:(unsigned int)aModifierFlags
{
struct HotKeyMappingEntry * theFoundEntry = NULL;
NSHashTable * theHashTable = [NDHotKeyEvent allHotKeyEvents];
if( theHashTable )
{
struct HotKeyMappingEntry theDummyEntry;
theDummyEntry.keyCode = aKeyCode;
theDummyEntry.modifierFlags = aModifierFlags;
theDummyEntry.hotKeyEvent = nil;
NDHotKeyEventLock;
theFoundEntry = NSHashGet( theHashTable, (void*)&theDummyEntry);
if( theFoundEntry != NULL )
[[theFoundEntry->hotKeyEvent retain] autorelease];
NDHotKeyEventUnlock;
}
return (theFoundEntry) ? theFoundEntry->hotKeyEvent : nil;
}
/*
* +hotKeyWithKeyCode:character:modifierFlags:
*/
+ (id)hotKeyWithKeyCode:(unsigned short)aKeyCode character:(unichar)aChar modifierFlags:(unsigned int)aModifierFlags
{
return [self hotKeyWithKeyCode:aKeyCode character:aChar modifierFlags:aModifierFlags target:nil selector:NULL];
}
/*
* +hotKeyWithKeyCode:character:modifierFlags:target:selector:
*/
+ (id)hotKeyWithKeyCode:(unsigned short)aKeyCode character:(unichar)aChar modifierFlags:(unsigned int)aModifierFlags target:(id)aTarget selector:(SEL)aSelector
{
return [[[self alloc] initWithKeyCode:aKeyCode character:aChar modifierFlags:aModifierFlags target:aTarget selector:aSelector] autorelease];
}
+ (id)hotKeyWithWithPropertyList:(id)aPropertyList
{
return [[[self alloc] initWithPropertyList:aPropertyList] autorelease];
}
+ (NSString *)description
{
NSHashTable * theHashTable = [NDHotKeyEvent allHotKeyEvents];
NSString * theDescription = nil;
if( theHashTable )
{
NDHotKeyEventLock;
theDescription = NSStringFromHashTable(theHashTable);
NDHotKeyEventUnlock;
}
return theDescription;
}
/*
* -init
*/
- (id)init
{
[self release];
NSAssert( NO, @"You can not initialize a Hot Key with the init method" );
return nil;
}
/*
* -initWithKeyCode:character:modifierFlags:
*/
- (id)initWithKeyCode:(unsigned short)aKeyCode character:(unichar)aChar modifierFlags:(unsigned int)aModifierFlags
{
return [self initWithKeyCode:aKeyCode character:aChar modifierFlags:aModifierFlags target:nil selector:NULL];
}
/*
* -initWithKeyCode:character:modifierFlags:target:selector:
*/
- (id)initWithKeyCode:(unsigned short)aKeyCode character:(unichar)aChar modifierFlags:(unsigned int)aModifierFlags target:(id)aTarget selector:(SEL)aSelector
{
if( (self = [super init]) )
{
keyCode = aKeyCode;
character = aChar;
modifierFlags = aModifierFlags;
target = aTarget;
selectorReleased = aSelector;
currentEventType = NDHotKeyNoEvent;
isEnabled.collective = YES;
if( ![self addHotKey] )
{
[self release];
self = nil;
}
}
else
{
if( aModifierFlags == 0 )
NSLog(@"All hot keys must have at least one modifer key");
[self release];
self = nil;
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
if( self = [super init] )
{
if( [aDecoder allowsKeyedCoding] )
{
keyCode = [[aDecoder decodeObjectForKey:kArchivingKeyCodeKey] unsignedShortValue];
character = [[aDecoder decodeObjectForKey:kArchivingCharacterKey] unsignedShortValue];
modifierFlags = [[aDecoder decodeObjectForKey:kArchivingModifierFlagsKey] unsignedIntValue];
selectorReleased = NSSelectorFromString( [aDecoder decodeObjectForKey:kArchivingSelectorReleasedCodeKey] );
selectorPressed = NSSelectorFromString( [aDecoder decodeObjectForKey:kArchivingSelectorPressedCodeKey] );
}
else
{
[aDecoder decodeValueOfObjCType:@encode(unsigned short) at:&keyCode];
[aDecoder decodeValueOfObjCType:@encode(unichar) at:&character];
[aDecoder decodeValueOfObjCType:@encode(unsigned int) at:&modifierFlags];
selectorReleased = NSSelectorFromString( [aDecoder decodeObject] );
selectorPressed = NSSelectorFromString( [aDecoder decodeObject] );
}
if( ![self addHotKey] )
{
[self release];
self = nil;
}
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)anEncoder
{
if( [anEncoder allowsKeyedCoding] )
{
[anEncoder encodeObject:[NSNumber numberWithUnsignedShort:keyCode] forKey:kArchivingKeyCodeKey];
[anEncoder encodeObject:[NSNumber numberWithUnsignedShort:character] forKey:kArchivingCharacterKey];
[anEncoder encodeObject:[NSNumber numberWithUnsignedInt:modifierFlags] forKey:kArchivingModifierFlagsKey];
[anEncoder encodeObject:NSStringFromSelector( selectorReleased ) forKey:kArchivingSelectorReleasedCodeKey];
[anEncoder encodeObject:NSStringFromSelector( selectorPressed ) forKey:kArchivingSelectorPressedCodeKey];
}
else
{
[anEncoder encodeValueOfObjCType:@encode(unsigned short) at:&keyCode];
[anEncoder encodeValueOfObjCType:@encode(unichar) at:&character];
[anEncoder encodeValueOfObjCType:@encode(unsigned int) at:&modifierFlags];
[anEncoder encodeObject:NSStringFromSelector( selectorReleased )];
[anEncoder encodeObject:NSStringFromSelector( selectorPressed )];
}
}
- (id)initWithPropertyList:(id)aPropertyList
{
if( aPropertyList )
{
NSString * theCharacter;
NSNumber * theKeyCode,
* theModiferFlag;
SEL theKeyPressedSelector,
theKeyReleasedSelector;
theKeyCode = [aPropertyList objectForKey:kArchivingKeyCodeKey];
theCharacter = [aPropertyList objectForKey:kArchivingCharacterKey];
theModiferFlag = [aPropertyList objectForKey:kArchivingModifierFlagsKey];
theKeyPressedSelector = NSSelectorFromString([aPropertyList objectForKey:kArchivingSelectorPressedCodeKey]);
theKeyReleasedSelector = NSSelectorFromString([aPropertyList objectForKey:kArchivingSelectorReleasedCodeKey]);
self = [self initWithKeyCode:[theKeyCode unsignedShortValue] character:[theCharacter characterAtIndex:0] modifierFlags:[theModiferFlag unsignedIntValue]];
}
else
{
[self release];
self = nil;
}
return self;
}
- (id)propertyList
{
return [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithUnsignedShort:[self keyCode]], kArchivingKeyCodeKey,
[NSString stringWithCharacters:&character length:1] , kArchivingCharacterKey,
[NSNumber numberWithUnsignedInt:[self modifierFlags]], kArchivingModifierFlagsKey,
NSStringFromSelector( selectorPressed ), kArchivingSelectorPressedCodeKey,
NSStringFromSelector( selectorReleased ), kArchivingSelectorReleasedCodeKey,
nil];
}
/*
* -release
*/
- (oneway void)release
{
/*
* We need to remove the hot key from the hash table before it's retain count reaches zero
*/
if( [self retainCount] == 1 )
{
NSHashTable * theHashTable = [NDHotKeyEvent allHotKeyEvents];
if( theHashTable )
{
struct HotKeyMappingEntry theDummyEntry;
theDummyEntry.keyCode = [self keyCode];
theDummyEntry.modifierFlags = [self modifierFlags];
theDummyEntry.hotKeyEvent = nil;
NDHotKeyEventLock;
if( [self retainCount] == 1 ) // check again because it might have changed
{
id theHotKeyEvent = NSHashGet( theHashTable, (void*)&theDummyEntry );
if( theHotKeyEvent )
NSHashRemove( theHashTable, theHotKeyEvent );
}
NDHotKeyEventUnlock;
}
}
else
[super release];
}
- (void)dealloc
{
if( UnregisterEventHotKey( reference ) != noErr ) // in lock from release
NSLog( @"Failed to unregister hot key %@", self );
[super dealloc];
}
/*
* -setEnabled:
*/
- (BOOL)setEnabled:(BOOL)aFlag
{
BOOL theResult = YES;
if( [NDHotKeyEvent install] )
{
/*
* if individual and collective YES then currently ON, otherwise currently off
*/
NDHotKeyEventLock;
if( aFlag == YES && isEnabled.collective == YES && isEnabled.individual == NO )
{
theResult = (switchHotKey( self, YES ) == noErr);
}
else if( aFlag == NO && isEnabled.collective == YES && isEnabled.individual == YES )
{
theResult = (switchHotKey( self, NO ) == noErr);
}
NDHotKeyEventUnlock;
if( theResult )
isEnabled.individual = aFlag;
else
NSLog(@"%s failed ", aFlag ? "enable" : "disable" );
}
else
theResult = NO;
return theResult;
}
/*
* -isEnabled
*/
- (BOOL)isEnabled
{
return isEnabled.individual && isEnabled.collective;
}
/*
* -target
*/
- (id)target
{
return target;
}
/*
* -selector
*/
- (SEL)selector
{
return selectorReleased;
}
/*
* -selectorReleased
*/
- (SEL)selectorReleased
{
return selectorReleased;
}
/*
* -selectorPressed
*/
- (SEL)selectorPressed
{
return selectorPressed;
}
/*
* -currentEventType
* (NDHotKeyNoEvent | NDHotKeyPressedEvent | NDHotKeyReleasedEvent)
*/
- (int)currentEventType
{
return currentEventType;
}
/*
* -setTarget:selector:
*/
- (BOOL)setTarget:(id)aTarget selector:(SEL)aSelector
{
return [self setTarget:aTarget selectorReleased:aSelector selectorPressed:(SEL)0];
}
/*
* -setTarget:selectorReleased:selectorPressed:
*/
- (BOOL)setTarget:(id)aTarget selectorReleased:(SEL)aSelectorReleased selectorPressed:(SEL)aSelectorPressed
{
[self setEnabled:NO];
if( target && target != aTarget )
{
[self setEnabled:NO];
if( ![target respondsToSelector:@selector(targetWillChangeToObject:forHotKeyEvent:)] || [target targetWillChangeToObject:aTarget forHotKeyEvent:self] )
{
target = aTarget;
selectorReleased = aSelectorReleased;
selectorPressed = aSelectorPressed;
}
}
else
{
target = aTarget;
selectorReleased = aSelectorReleased;
selectorPressed = aSelectorPressed;
}
return target == aTarget; // was change succesful
}
/*
* -performHotKeyReleased
*/
- (void)performHotKeyReleased
{
NSAssert( target, @"NDHotKeyEvent tried to perfrom release with no target" );
if( selectorReleased && [target respondsToSelector:selectorReleased])
{
currentEventType = NDHotKeyReleasedEvent;
[target performSelector:selectorReleased withObject:self];
currentEventType = NDHotKeyNoEvent;
}
}
/*
* -performHotKeyPressed
*/
- (void)performHotKeyPressed
{
NSAssert( target, @"NDHotKeyEvent tried to perfrom press with no target" );
if( selectorPressed && [target respondsToSelector:selectorPressed])
{
currentEventType = NDHotKeyPressedEvent;
[target performSelector:selectorPressed withObject:self];
currentEventType = NDHotKeyNoEvent;
}
}
/*
* -keyCode
*/
- (unsigned short)keyCode
{
return keyCode;
}
/*
* -character
*/
- (unichar)character
{
return character;
}
/*
* -modifierFlags
*/
- (unsigned int)modifierFlags
{
return modifierFlags;
}
/*
* -stringValue
*/
- (NSString *)stringValue
{
NSString * theStringValue = nil;
NDHotKeyEventLock;
theStringValue = stringForKeyCodeAndModifierFlags( [self keyCode], [self character], [self modifierFlags] );
NDHotKeyEventUnlock;
return theStringValue;
}
/*
* -isEqual:
*/
- (BOOL)isEqual:(id)anObject
{
return [super isEqual:anObject] || ([anObject isKindOfClass:[self class]] == YES && [self keyCode] == [(NDHotKeyEvent*)anObject keyCode] && [self modifierFlags] == [anObject modifierFlags]);
}
/*
* -hash
*/
- (unsigned int)hash
{
return ((unsigned int)keyCode & ~modifierFlags) | (modifierFlags & ~((unsigned int)keyCode)); // xor
}
/*
* -description
*/
- (NSString *)description
{
return [NSString stringWithFormat:@"{\n\tKey Combination: %@,\n\tEnabled: %s\n\tTarget: %@\n\tKey Press Selector: %@\n\tKey Release Selector: %@\n}\n", [self stringValue],
[self isEnabled] ? "yes" : "no",
[self target],
NSStringFromSelector([self selectorPressed]),
NSStringFromSelector([self selectorReleased])];
}
/*
* cocoaModifierFlagsToCarbonModifierFlags()
*/
unsigned int cocoaModifierFlagsToCarbonModifierFlags( unsigned int aModifierFlags )
{
unsigned int theCarbonModifierFlags = 0;
if(aModifierFlags & NSShiftKeyMask)
theCarbonModifierFlags |= shiftKey;
if(aModifierFlags & NSControlKeyMask)
theCarbonModifierFlags |= controlKey;
if(aModifierFlags & NSAlternateKeyMask)
theCarbonModifierFlags |= optionKey;
if(aModifierFlags & NSCommandKeyMask)
theCarbonModifierFlags |= cmdKey;
return theCarbonModifierFlags;
}
/*
* eventHandlerCallback()
*/
pascal OSErr eventHandlerCallback( EventHandlerCallRef anInHandlerCallRef, EventRef anInEvent, void * anInUserData )
{
// NSHashTable * allHotKeyEvents = (NSHashTable *)anInUserData;
EventHotKeyID theHotKeyID;
OSStatus theError;
NSCAssert( GetEventClass( anInEvent ) == kEventClassKeyboard, @"Got event that is not a hot key event" );
theError = GetEventParameter( anInEvent, kEventParamDirectObject, typeEventHotKeyID, NULL, sizeof(EventHotKeyID), NULL, &theHotKeyID );
if( theError == noErr )
{
NDHotKeyEvent * theHotKeyEvent;
UInt32 theEventKind;
NSCAssert( [NDHotKeyEvent signature] == theHotKeyID.signature, @"Got hot key event with wrong signature" );
theHotKeyEvent = (NDHotKeyEvent*)theHotKeyID.id;
theEventKind = GetEventKind( anInEvent );
if( kEventHotKeyPressed == theEventKind )
{
[theHotKeyEvent performHotKeyPressed];
}
else if( kEventHotKeyReleased == theEventKind )
{
[theHotKeyEvent performHotKeyReleased];
}
}
return theError;
}
/*
* hashValueHashFunction()
*/
unsigned hashValueHashFunction( NSHashTable * aTable, const void * aHotKeyEntry )
{
struct HotKeyMappingEntry * theHotKeyEntry;
unsigned int theKeyCode,
theModifiers;
theHotKeyEntry = (struct HotKeyMappingEntry*)aHotKeyEntry;
theKeyCode = (unsigned int)theHotKeyEntry->keyCode;
theModifiers = (unsigned int)theHotKeyEntry->modifierFlags;
return theKeyCode ^ theModifiers; // xor
}
/*
* isEqualHashFunction()
*/
BOOL isEqualHashFunction( NSHashTable * aTable, const void * aFirstHotKeyEntry, const void * aSecondHotKeyEntry)
{
struct HotKeyMappingEntry * theFirst,
* theSecond;
theFirst = (struct HotKeyMappingEntry*)aFirstHotKeyEntry;
theSecond = (struct HotKeyMappingEntry*)aSecondHotKeyEntry;
return theFirst->keyCode == theSecond->keyCode && theFirst->modifierFlags == theSecond->modifierFlags;
}
/*
* describeHashFunction()
*/
NSString * describeHashFunction( NSHashTable * aTable, const void * aHotKeyEntry )
{
NDHotKeyEvent * theHotKey;
theHotKey = ((struct HotKeyMappingEntry*)aHotKeyEntry)->hotKeyEvent;
return [theHotKey description];
}
@end
@implementation NDHotKeyEvent (Private)
/*
* +allHotKeyEvents
*/
+ (NSHashTable *)allHotKeyEvents
{
if( allHotKeyEvents == NULL )
{
NSHashTableCallBacks theHashCallBacks;
theHashCallBacks.hash = hashValueHashFunction;
theHashCallBacks.isEqual = isEqualHashFunction;
theHashCallBacks.retain = NULL;
theHashCallBacks.release = NULL;
theHashCallBacks.describe = describeHashFunction;
NDHotKeyEventLock;
if( allHotKeyEvents == NULL )
allHotKeyEvents = NSCreateHashTableWithZone( theHashCallBacks, 0, NULL);
NDHotKeyEventUnlock;
}
return allHotKeyEvents;
}
/*
* -addHotKey
*/
- (BOOL)addHotKey
{
BOOL theSuccess = NO;
NSHashTable * theHashTable = [NDHotKeyEvent allHotKeyEvents];
if( theHashTable )
{
struct HotKeyMappingEntry * theEntry;
theEntry = (struct HotKeyMappingEntry *)malloc(sizeof(struct HotKeyMappingEntry));
theEntry->keyCode = [self keyCode];
theEntry->modifierFlags = [self modifierFlags];
theEntry->hotKeyEvent = self;
NDHotKeyEventLock;
theSuccess = NSHashInsertIfAbsent( theHashTable, (void*)theEntry ) == NULL;
NDHotKeyEventUnlock;
}
return theSuccess;
}
/*
* -removeHotKey
*/
- (void)removeHotKey
{
[self setEnabled:NO];
NSHashTable * theHashTable = [NDHotKeyEvent allHotKeyEvents];
if( theHashTable )
{
struct HotKeyMappingEntry theDummyEntry;
id theHotKeyEvent;
theDummyEntry.keyCode = [self keyCode];
theDummyEntry.modifierFlags = [self modifierFlags];
theDummyEntry.hotKeyEvent = nil;
NDHotKeyEventLock;
theHotKeyEvent = NSHashGet( theHashTable, (void*)&theDummyEntry);
if( theHotKeyEvent )
NSHashRemove( theHashTable, theHotKeyEvent );
NDHotKeyEventUnlock;
}
}
/*
* setCollectiveEnabled:
*/
- (BOOL)setCollectiveEnabled:(BOOL)aFlag
{
BOOL theResult = YES;
if( [NDHotKeyEvent install] )
{
/*
* if individual and collective YES then currently ON, otherwise currently off
*/
NDHotKeyEventLock;
if( aFlag == YES && isEnabled.collective == NO && isEnabled.individual == YES )
{
theResult = (switchHotKey( self, YES ) == noErr);
}
else if( aFlag == NO && isEnabled.collective == YES && isEnabled.individual == YES )
{
theResult = (switchHotKey( self, NO ) == noErr);
}
NDHotKeyEventUnlock;
if( theResult )
isEnabled.collective = aFlag;
else
NSLog(@"%s %@ failed", aFlag ? "enable" : "disable",self );
}
else
theResult = NO;
return theResult;
}
/*
* collectiveEnable()
*/
- (BOOL)collectiveEnable
{
return isEnabled.collective;
}
/*
* switchHotKey()
*/
static OSStatus switchHotKey( NDHotKeyEvent * self, BOOL aFlag )
{
OSStatus theError;
if( aFlag )
{
EventHotKeyID theHotKeyID;
theHotKeyID.signature = [NDHotKeyEvent signature];
theHotKeyID.id = (unsigned int)self;
NSCAssert( theHotKeyID.signature, @"HotKeyEvent signature has not been set yet" );
NSCParameterAssert(sizeof(unsigned long) >= sizeof(id) );
theError = RegisterEventHotKey( self->keyCode, cocoaModifierFlagsToCarbonModifierFlags(self->modifierFlags), theHotKeyID, GetEventDispatcherTarget(), 0, &self->reference );
}
else
{
theError = UnregisterEventHotKey( self->reference );
}
return theError;
}
@end
/*
* stringForKeyCodeAndModifierFlags()
*/
NSString * stringForKeyCodeAndModifierFlags( unsigned short aKeyCode, unichar aChar, unsigned int aModifierFlags )
{
NSString * stringForCharacter( const unsigned short aKeyCode, unichar aCharacter );
NSString * stringForModifiers( unsigned int aModifierFlags );
return [stringForModifiers(aModifierFlags) stringByAppendingString:stringForCharacter( aKeyCode, aChar )];
}
unichar unicodeForFunctionKey( UInt32 aKeyCode );
/*
* unicharForKeyCode()
*/
unichar unicharForKeyCode( unsigned short aKeyCode )
{
static UInt32 theState = 0;
const void * theKeyboardLayoutData;
KeyboardLayoutRef theCurrentKeyBoardLayout;
UInt32 theChar = kNullCharCode;
if( KLGetCurrentKeyboardLayout( &theCurrentKeyBoardLayout ) == noErr && KLGetKeyboardLayoutProperty( theCurrentKeyBoardLayout, kKLKCHRData, &theKeyboardLayoutData) == noErr )
{
theChar = KeyTranslate ( theKeyboardLayoutData, aKeyCode, &theState );
switch( theChar )
{
case kHomeCharCode: theChar = NSHomeFunctionKey; break;
// case kEnterCharCode: theChar = ; break;
case kEndCharCode: theChar = NSEndFunctionKey; break;
case kHelpCharCode: theChar = NSHelpFunctionKey; break;
// case kBellCharCode: theChar = ; break;
// case kBackspaceCharCode: theChar = ; break;
// case kTabCharCode: theChar = ; break;
// case kLineFeedCharCode: theChar = ; break;
case kPageUpCharCode: theChar = NSPageUpFunctionKey; break;
case kPageDownCharCode: theChar = NSPageDownFunctionKey; break;
// case kReturnCharCode: theChar = ; break;
case kFunctionKeyCharCode: theChar = unicodeForFunctionKey( aKeyCode ); break;
// case kCommandCharCode: theChar = ; break;
// case kCheckCharCode: theChar = ; break;
// case kDiamondCharCode : theChar = ; break;
// case kAppleLogoCharCode: theChar = ; break;
// case kEscapeCharCode: theChar = ; break;
case kClearCharCode:
theChar = (aKeyCode==0x47) ? NSInsertFunctionKey : theChar;
break;
case kLeftArrowCharCode: theChar = NSLeftArrowFunctionKey; break;
case kRightArrowCharCode: theChar = NSRightArrowFunctionKey; break;
case kUpArrowCharCode: theChar = NSUpArrowFunctionKey; break;
case kDownArrowCharCode: theChar = NSDownArrowFunctionKey; break;
// case kSpaceCharCode: theChar = ; break;
case kDeleteCharCode: theChar = NSDeleteCharFunctionKey; break;
// case kBulletCharCode: theChar = ; break;
// case kNonBreakingSpaceCharCode: theChar = ; break;
}
}
return theChar;
}
unichar unicodeForFunctionKey( UInt32 aKeyCode )
{
switch( aKeyCode )
{
case 0x7A: return NSF1FunctionKey;
case 0x78: return NSF2FunctionKey;
case 0x63: return NSF3FunctionKey;
case 0x76: return NSF4FunctionKey;
case 0x60: return NSF5FunctionKey;
case 0x61: return NSF6FunctionKey;
case 0x62: return NSF7FunctionKey;
case 0x64: return NSF8FunctionKey;
case 0x65: return NSF9FunctionKey;
case 0x6D: return NSF10FunctionKey;
case 0x67: return NSF11FunctionKey;
case 0x6F: return NSF12FunctionKey;
// BLTRMod
case 0x69: return NSF13FunctionKey;
case 0x6B: return NSF14FunctionKey;
case 0x71: return NSF15FunctionKey;
// BLTRModEnd
default: return 0x00;
}
}
NSString * stringForCharacter( const unsigned short aKeyCode, unichar aCharacter )
{
NSString * theString = nil;
if (!aCharacter)
aCharacter = unicharForKeyCode(aKeyCode);
switch( aCharacter )
{
case NSUpArrowFunctionKey:
aCharacter = 0x2191;
theString = [NSString stringWithCharacters:&aCharacter length:1];
break;
case NSDownArrowFunctionKey:
aCharacter = 0x2193;
theString = [NSString stringWithCharacters:&aCharacter length:1];
break;
case NSLeftArrowFunctionKey:
aCharacter = 0x2190;
theString = [NSString stringWithCharacters:&aCharacter length:1];
break;
case NSRightArrowFunctionKey:
aCharacter = 0x2192;
theString = [NSString stringWithCharacters:&aCharacter length:1];
break;
case NSF1FunctionKey: theString = @"F1"; break;
case NSF2FunctionKey: theString = @"F2"; break;