To submit a new ticket, please login with guest/spamfilter. This is to prevent advertising bots from creating tickets.

Current version: 0.91


Download it now!

- or -
Get the latest source version

root/trunk/PTHotKeyCenter.m

Revision 31, 4.8 kB (checked in by ssilver, 1 week ago)

Hot key recording is now working.

Line 
1 //
2 //  PTHotKeyCenter.m
3 //  Protein
4 //
5 //  Created by Quentin Carnicelli on Sat Aug 02 2003.
6 //  Copyright (c) 2003 Quentin D. Carnicelli. All rights reserved.
7 //
8
9 #import "PTHotKeyCenter.h"
10 #import "PTHotKey.h"
11 #import "PTKeyCombo.h"
12 #import <Carbon/Carbon.h>
13
14 @interface PTHotKeyCenter (Private)
15 - (void)_updateEventHandler;
16 - (void)_hotKeyDown: (PTHotKey*)hotKey;
17 - (void)_hotKeyUp: (PTHotKey*)hotKey;
18 static OSStatus hotKeyEventHandler(EventHandlerCallRef inHandlerRef, EventRef inEvent, void* refCon );
19 @end
20
21 @implementation PTHotKeyCenter
22
23 static id _sharedHotKeyCenter = nil;
24
25 + (id)sharedCenter
26 {
27         if( _sharedHotKeyCenter == nil )
28         {
29                 _sharedHotKeyCenter = [[self alloc] init];
30         }
31        
32         return _sharedHotKeyCenter;
33 }
34
35 - (id)init
36 {
37         self = [super init];
38        
39         if( self )
40         {
41                 mHotKeys = [[NSMutableDictionary alloc] init];
42         }
43        
44         return self;
45 }
46
47 - (void)dealloc
48 {
49         [mHotKeys release];
50         [super dealloc];
51 }
52
53 #pragma mark -
54
55 - (BOOL)registerHotKey: (PTHotKey*)hotKey
56 {
57     //NSLog(@"registerHotKey: %@", hotKey);
58         OSStatus err;
59         EventHotKeyID hotKeyID;
60         EventHotKeyRef carbonHotKey;
61    
62         if([mHotKeys objectForKey:[hotKey name]])
63     {
64         //NSLog(@"key already registered, deregistering first");
65         [self unregisterHotKey: hotKey];
66     }
67         if( [[hotKey keyCombo] isValidHotKeyCombo] == NO )
68     {
69         //NSLog(@"not valid keycombo:");
70         return YES;
71     }
72                
73        
74         hotKeyID.signature = 'PTHk';
75         hotKeyID.id = (long)hotKey;
76         //NSLog(@"registering...");
77         err = RegisterEventHotKey(  [[hotKey keyCombo] keyCode],
78                                                                 [[hotKey keyCombo] modifiers],
79                                                                 hotKeyID,
80                                                                 GetEventDispatcherTarget(),
81                                                                 nil,
82                                                                 &carbonHotKey );
83
84         if( err )
85     {
86         //NSLog(@"error --");
87         return NO;
88     }
89                
90
91     [hotKey setCarbonHotKey:carbonHotKey];
92         [mHotKeys setObject: hotKey forKey: [hotKey name]];
93
94         [self _updateEventHandler];
95         //NSLog(@"Eo registerHotKey:");
96         return YES;
97 }
98
99 - (void)unregisterHotKey: (PTHotKey*)hotKey
100 {
101     //NSLog(@"unregisterHotKey: %@", hotKey);
102         OSStatus err;
103         EventHotKeyRef carbonHotKey;
104
105         if(![mHotKeys objectForKey:[hotKey name]])
106                 return;
107        
108         carbonHotKey = [hotKey carbonHotKey];
109         NSAssert( carbonHotKey != nil, @"" );
110
111         err = UnregisterEventHotKey( carbonHotKey );
112         //Watch as we ignore 'err':
113
114         [mHotKeys removeObjectForKey: [hotKey name]];
115        
116         [self _updateEventHandler];
117     //NSLog(@"Eo unregisterHotKey:");
118         //See that? Completely ignored
119 }
120
121 - (void) unregisterHotKeyForName:(NSString *)name
122 {
123     [self unregisterHotKey:[mHotKeys objectForKey:name]];
124 }
125
126 - (void) unregisterAllHotKeys;
127 {
128     NSEnumerator *enumerator = [mHotKeys objectEnumerator];
129     id thing;
130     while (thing = [enumerator nextObject])
131     {
132         [self unregisterHotKey:thing];
133     }
134 }
135
136 - (void) setHotKeyRegistrationForName:(NSString *)name enable:(BOOL)ena
137 {
138     if (ena)
139     {
140         [self registerHotKey:[mHotKeys objectForKey:name]];
141     } else
142     {
143         [self unregisterHotKey:[mHotKeys objectForKey:name]];
144     }
145 }
146
147 - (void) updateHotKey:(PTHotKey *)hk
148 {
149     [hk retain];
150     //NSLog(@"updateHotKey: %@", hk);
151     [self unregisterHotKey:[mHotKeys objectForKey:[hk name]]];
152     //NSLog(@"unreg'd: %@", hk);
153     [self registerHotKey:hk];
154     //NSLog(@"Eo updateHotKey:");
155 }
156
157 - (PTHotKey *) hotKeyForName:(NSString *)name
158 {
159     return [mHotKeys objectForKey:name];
160 }
161
162 - (NSArray*)allHotKeys
163 {
164         return [mHotKeys allValues];
165 }
166
167 #pragma mark -
168 - (void)_updateEventHandler
169 {
170         if( [mHotKeys count] && mEventHandlerInstalled == NO )
171         {
172                 EventTypeSpec eventSpec[2] = {
173                         { kEventClassKeyboard, kEventHotKeyPressed },
174                         { kEventClassKeyboard, kEventHotKeyReleased }
175                 };   
176
177                 InstallEventHandler( GetEventDispatcherTarget(),
178                                                          (EventHandlerProcPtr)hotKeyEventHandler,
179                                                          2, eventSpec, nil, nil);
180        
181                 mEventHandlerInstalled = YES;
182         }
183 }
184
185 - (void)_hotKeyDown: (PTHotKey*)hotKey
186 {
187         [hotKey invoke];
188 }
189
190 - (void)_hotKeyUp: (PTHotKey*)hotKey
191 {
192     //[hotKey uninvoke];
193 }
194
195 - (OSStatus)sendCarbonEvent: (EventRef)event
196 {
197         OSStatus err;
198         EventHotKeyID hotKeyID;
199         PTHotKey* hotKey;
200
201         NSAssert( GetEventClass( event ) == kEventClassKeyboard, @"Unknown event class" );
202
203         err = GetEventParameter(        event,
204                                                                 kEventParamDirectObject,
205                                                                 typeEventHotKeyID,
206                                                                 nil,
207                                                                 sizeof(EventHotKeyID),
208                                                                 nil,
209                                                                 &hotKeyID );
210         if( err )
211                 return err;
212        
213
214         NSAssert( hotKeyID.signature == 'PTHk', @"Invalid hot key id" );
215         NSAssert( hotKeyID.id != nil, @"Invalid hot key id" );
216
217         hotKey = (PTHotKey*)hotKeyID.id;
218
219         switch( GetEventKind( event ) )
220         {
221                 case kEventHotKeyPressed:
222             [self _hotKeyDown: hotKey];
223             break;
224
225                 case kEventHotKeyReleased:
226             [self _hotKeyUp: hotKey];
227             break;
228
229                 default:
230                         NSAssert( 0, @"Unknown event kind" );
231         }
232        
233         return noErr;
234 }
235
236 static OSStatus hotKeyEventHandler(EventHandlerCallRef inHandlerRef, EventRef inEvent, void* refCon )
237 {
238         return [[PTHotKeyCenter sharedCenter] sendCarbonEvent: inEvent];
239 }
240
241 @end
Note: See TracBrowser for help on using the browser.