github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

carpeaqua / SGHotKeysLib

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 21
    • 3
  • Source
  • Commits
  • Network (3)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (2)
    • 1.1
    • 1.0
Sending Request…
Click here to lend your support to: SGHotKeysLib and make a donation at www.pledgie.com ! Edit Pledgie Setup

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

A modernized fork of PTHotKeysLib for Mac OS X Leopard and beyond — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Added a retain for theKeyCombo in the setKeyCombo setter 
pburleson (author)
Wed Nov 11 16:18:41 -0800 2009
commit  0ec2bbb66a61ac0e402122a0aeaf4fe2063e430a
tree    8131561597422befb48c0471a929537b0138bf58
parent  376f1bee979e79b7e56bf3a9814f2c799a306e46
SGHotKeysLib / SGHotKeysLib / SGHotKeyCenter.m SGHotKeysLib/SGHotKeyCenter.m
100644 231 lines (174 sloc) 5.576 kb
edit raw blame history
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
//
// SGHotKeyCenter.m
// SGHotKeyCenter
//
// Created by Justin Williams on 7/26/09.
// Copyright 2009 Second Gear. All rights reserved.
//
 
#import <Carbon/Carbon.h>
#import "SGHotKeyCenter.h"
#import "SGHotKey.h"
#import "SGKeyCombo.h"
 
OSType const kHotKeySignature = 'SGHk';
 
@interface SGHotKeyCenter ()
- (SGHotKey *)_hotKeyForCarbonHotKey:(EventHotKeyRef)carbonHotKey;
- (EventHotKeyRef)_carbonHotKeyForHotKey:(SGHotKey *)hotKey;
 
- (void)_updateEventHandler;
- (void)_hotKeyDown: (SGHotKey *)hotKey;
- (void)_hotKeyUp: (SGHotKey *)hotKey;
static OSStatus hotKeyEventHandler(EventHandlerCallRef inHandlerRef, EventRef inEvent, void* refCon );
@end
 
 
static SGHotKeyCenter *sharedCenter = nil;
 
@implementation SGHotKeyCenter
 
+ (void)initialize {
if (!sharedCenter) {
sharedCenter = [[self alloc] init];
}
}
 
- (void)dealloc {
  [hotKeys release];
  [super dealloc];
}
 
+ (SGHotKeyCenter *)sharedCenter {
  return sharedCenter;
}
 
+ (id) allocWithZone:(NSZone *)zone {
  //Usually already set by +initialize.
  if (sharedCenter) {
    //The caller expects to receive a new object, so implicitly retain it to balance out the caller's eventual release message.
    return [sharedCenter retain];
  } else {
    //When not already set, +initialize is our caller—it's creating the shared instance. Let this go through.
    return [super allocWithZone:zone];
  }
}
 
- (id) init {
  if (!hasInited) {
    if ((self = [super init])) {
      //Initialize the instance here.
hotKeys = [[NSMutableDictionary alloc] init];
      hasInited = YES;
    }
  }
 
  return self;
}
 
- (BOOL)registerHotKey:(SGHotKey *)theHotKey {
  OSStatus error;
  EventHotKeyID hotKeyID;
  EventHotKeyRef carbonHotKey;
  NSValue *key = nil;
  
  if ([[self allHotKeys] containsObject:theHotKey])
    [self unregisterHotKey:theHotKey];
  
  if (![[theHotKey keyCombo] isValidHotKeyCombo])
    return YES;
  
  static UInt32 currentId = 0;
  hotKeyID.signature = kHotKeySignature;
  hotKeyID.id = ++currentId;
  
  theHotKey.hotKeyID = hotKeyID;
  
  error = RegisterEventHotKey(theHotKey.keyCombo.keyCode,
                              theHotKey.keyCombo.modifiers,
                              hotKeyID,
                              GetEventDispatcherTarget(),
                              0,
                              &carbonHotKey);
  
  if (error)
    return NO;
  
  key = [NSValue valueWithPointer:carbonHotKey];
  
  if (theHotKey && key)
    [hotKeys setObject:theHotKey forKey:key];
  
  [self _updateEventHandler];
  
  return YES;
}
 
 
- (void)unregisterHotKey:(SGHotKey *)theHotKey {
  EventHotKeyRef carbonHotKey;
  NSValue *key = nil;
  
  if (![[self allHotKeys] containsObject:theHotKey])
return;
 
carbonHotKey = [self _carbonHotKeyForHotKey:theHotKey];
NSAssert(carbonHotKey != nil, @"");
  
UnregisterEventHotKey(carbonHotKey);
  
key = [NSValue valueWithPointer:carbonHotKey];
[hotKeys removeObjectForKey:key];
 
[self _updateEventHandler];
}
 
- (NSArray *)allHotKeys {
  return [hotKeys allValues];
}
 
 
- (SGHotKey *)hotKeyWithIdentifier:(id)theIdentifier {
  if (!theIdentifier)
    return nil;
  
  for (SGHotKey *hotKey in [self allHotKeys]) {
    if([[hotKey identifier] isEqual:theIdentifier] )
return hotKey;
  }
  
  return nil;
}
 
- (OSStatus)sendCarbonEvent:(EventRef)event {
  OSStatus error;
  EventHotKeyID hotKeyID;
  SGHotKey *hotKey = nil;
 
  NSAssert(GetEventClass(event) == kEventClassKeyboard, @"Unknown event class");
 
  error = GetEventParameter(event,
                            kEventParamDirectObject,
                            typeEventHotKeyID,
                            nil,
                            sizeof(EventHotKeyID),
                            nil,
                            &hotKeyID);
  if (error)
    return error;
 
  NSAssert(hotKeyID.signature == kHotKeySignature, @"Invalid hot key id");
  NSAssert(hotKeyID.id != 0, @"Invalid hot key id");
 
  for (SGHotKey *thisHotKey in [self allHotKeys]) {
    if ([thisHotKey matchesHotKeyID:hotKeyID]) {
      hotKey = thisHotKey;
      break;
    }
  }
 
  switch (GetEventKind(event)) {
    case kEventHotKeyPressed:
      [self _hotKeyDown:hotKey];
      break;
 
    case kEventHotKeyReleased:
      [self _hotKeyUp:hotKey];
      break;
      
    default:
      NSAssert(0, @"Unknown event kind");
      break;
  }
 
  return noErr;
}
 
- (SGHotKey *)_hotKeyForCarbonHotKey:(EventHotKeyRef)carbonHotKey {
  NSValue *key = [NSValue valueWithPointer:carbonHotKey];
return [hotKeys objectForKey:key];
}
 
- (EventHotKeyRef)_carbonHotKeyForHotKey:(SGHotKey *)hotKey {
  NSArray *values;
  NSValue *value;
 
  values = [hotKeys allKeysForObject:hotKey];
  NSAssert([values count] == 1, @"Failed to find Carbon Hotkey for SGHotKey");
 
  value = [values lastObject];
 
  return (EventHotKeyRef)[value pointerValue];
}
 
- (void)_updateEventHandler {
if ([hotKeys count] && eventHandlerInstalled == NO) {
EventTypeSpec eventSpec[2] = {
{ kEventClassKeyboard, kEventHotKeyPressed },
{ kEventClassKeyboard, kEventHotKeyReleased }
};
    
InstallEventHandler(GetEventDispatcherTarget(),
                        (EventHandlerProcPtr)hotKeyEventHandler,
                        2, eventSpec, nil, nil);
    
eventHandlerInstalled = YES;
}
}
 
- (void)_hotKeyDown:(SGHotKey *)hotKey {
  [hotKey invoke];
}
 
- (void)_hotKeyUp:(SGHotKey *)hotKey {
  // Nothing!
}
 
static OSStatus hotKeyEventHandler(EventHandlerCallRef theHandlerRef, EventRef theEvent, void *userData ) {
  return [[SGHotKeyCenter sharedCenter] sendCarbonEvent:theEvent];
}
@end
 
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server