-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathNDHotKeyEvent.h
executable file
·427 lines (387 loc) · 18.6 KB
/
NDHotKeyEvent.h
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
/*!
@header NDHotKeyEvent.h
@abstract Header file for the class <tt>NDHotKeyEvent</tt>
@discussion <p><tt>NDHotKeyEvent</tt> provides a thread safe Objective-C interface to HotKey events as well as some aditional feature to key track of all the hot keys in your application.</p>
<p><h4>Thread Saftey</h4>By default the class object <tt>NDHotKeyEvent</tt> is not thread safe as the underlying functions that it relies on are not thread safe and the mechanism for keeping track of all of the <tt>NDHotKeyEvent</tt> instances is not thread safe either. Thread saftey can be enable be defining the flag <tt>NDHotKeyEventThreadSafe</tt> before compiling.</p>
<p>Even with the flag <tt>NDHotKeyEventThreadSafe</tt> defined instances of <tt>NDHotKeyEvent</tt> will still not be thread safe, that is, it is safe to invoke methods of different instance with different threads as well as class methods, but it is not safe to invoke methods of the same instance with different threads.</p>
<p>The functions <tt>stringForKeyCodeAndModifierFlags</tt> and <tt>unicharForKeyCode</tt> are never thread safe.</p>
<p>Created by Nathan Day on Wed Feb 26 2003.<br>
Copyright © 2002 Nathan Day. All rights reserved.</p>
*/
#import <Cocoa/Cocoa.h>
#import <Carbon/Carbon.h>
/*!
@defined NDHotKeyEventThreadSafe
@abstract A flag to enable thread safety.
@discussion By default the class object <tt>NDHotKeyEvent</tt> is not thread safe. Defining the this flag will make th class methods of <tt>NDHotKeyEvent</tt> thread safe, see introduction for more details..
*/
enum
{
/*!
@const NDHotKeyNoEvent
@abstract A value returned from the method <tt>-[NDHotKeyEvent currentEventType]</tt>
@discussion This value is returned if the hot key has not been pressed yet.
*/
NDHotKeyNoEvent = 0,
/*!
@const NDHotKeyPressedEvent
@abstract A value returned from the method <tt>-[NDHotKeyEvent currentEventType]</tt>
@discussion This value is returned if hot key was pressed last.
*/
NDHotKeyPressedEvent,
/*!
@const NDHotKeyReleasedEvent
@abstract A value returned from the method <tt>-[NDHotKeyEvent currentEventType]</tt>
@discussion This value is returned if hot key was released last.
*/
NDHotKeyReleasedEvent
};
/*!
@const NDHotKeyDefaultSignature
@abstract The default signature
@discussion This is the default signature that will be used if you start using <tt>NDHotKeyEvent</tt> without setting the signature first.
*/
extern const OSType NDHotKeyDefaultSignature;
/*!
@class NDHotKeyEvent
@abstract Class to represent a HotKey
@discussion <p>This class is a wrapper for Carbon Event HotKeys and provides some feature to key track of all the hot keys in your application. It can be used to be notified of key down as well as key up evernts and when a hot key is being taken by another object (see the protocol <tt>NDHotKeyEventTragetWillChange</tt>)</p>
*/
@interface NDHotKeyEvent : NSObject <NSCoding>
{
@private
EventHotKeyRef reference;
unsigned short keyCode;
unichar character;
unsigned int modifierFlags;
int currentEventType;
id target;
SEL selectorReleased,
selectorPressed;
struct
{
unsigned individual : 1;
unsigned collective : 1;
} isEnabled;
}
/*!
@method install
@abstract Install the event key handler
@discussion <tt>install</tt> is called before hot keys can be used. You normally don't need to invoke this method your self but in a multithreaded you might want to invoke this method before creating any threads. <tt>install</tt> is designed to be thread safe but the effects of calling Apples <tt>InstallEventHandler()</tt> funtion from anything other than the main thread is unknown.
@result Returns true if <tt>install</tt> succeeded.
*/
+ (BOOL)install;
/*!
@method setSignature:
@abstract Set the hot key signature for this application
@discussion This should only be called once, before trying to enable any hot keys.
@param signature The four char code signature to identify all hot keys for this application, could your applications signature.
*/
+ (void)setSignature:(OSType)signature;
/*!
@method signature
@abstract Get the hot key signature for this application
@discussion Used to identify the hot key handler for this application.
@result The four char code signature.
*/
+ (OSType)signature;
/*!
@method setAllEnabled:
@abstract Set enabled for all instances of <tt>NDHotKeyEvent</tt>
@discussion Used to enable or disable all hot keys. This method is not the same as sending the message <tt>setEnabled:</tt> to every single <tt>NDHotKeyEvent</tt> instance. Enabling with this method only enables the hot keys that where enable prior to using this method to disable all hot keys.
@param flag <tt>YES</tt> to enable, <tt>NO</tt> to disable.
@result Returns <tt>YES</tt> if succesful.
*/
+ (BOOL)setAllEnabled:(BOOL)flag;
/*!
@method isEnabledKeyCode:modifierFlags:
@abstract Is hot key combination enabled.
@abstract Test to see if a key code and modifier flaf combination are enabled.
@param keyCode The key code used by the keyboard, can vary across hardware.
@param modifierFlags The modifer flags, ( <tt>NSCommandKeyMask</tt>, <tt>NSControlKeyMask</tt>, <tt>NSAlternateKeyMask</tt>, <tt>NSShiftKeyMask</tt> ).
@result Returns <tt>YES</tt> if enabled.
*/
+ (BOOL)isEnabledKeyCode:(unsigned short)keyCode modifierFlags:(unsigned int)modifierFlags;
/*!
@method getHotKeyForKeyCode:modifierFlags:
@abstract Get an <tt>NDHotKeyEvent</tt>
@discussion Gets a <tt>NDHotKeyEvent</tt> for the supplied key code and modifer flags by either finding one that has already been created or by creating a new one..
@param keyCode The key code used by the keyboard, can vary across hardware.
@param aChar The character, used for display purposes only.
@param modifierFlags The modifer flags, ( <tt>NSCommandKeyMask</tt>, <tt>NSControlKeyMask</tt>, <tt>NSAlternateKeyMask</tt>, <tt>NSShiftKeyMask</tt> ).
@result The <tt>NDHotKeyEvent</tt> obejct or nil if failure.
*/
+ (NDHotKeyEvent *)getHotKeyForKeyCode:(unsigned short)keyCode character:(unichar)aChar modifierFlags:(unsigned int)modifierFlags;
/*!
@method findHotKeyForKeyCode:modifierFlags:
@abstract Find an <tt>NDHotKeyEvent</tt>
@discussion Finds the <tt>NDHotKeyEvent</tt> for the supplied key code and modifer flags.
@param keyCode The key code used by the keyboard, can vary across hardware.
@param modifierFlags The modifer flags, ( <tt>NSCommandKeyMask</tt>, <tt>NSControlKeyMask</tt>, <tt>NSAlternateKeyMask</tt>, <tt>NSShiftKeyMask</tt> ).
@result The <tt>NDHotKeyEvent</tt> obejct or nil if none found.
*/
+ (NDHotKeyEvent *)findHotKeyForKeyCode:(unsigned short)keyCode modifierFlags:(unsigned int)modifierFlags;
/*!
@method hotKeyWithKeyCode:character:modifierFlags:
@abstract Get a <tt>NDHotKeyEvent</tt> object.
@discussion Returns a new hot key for the supplied hot key combination, if there is already a hot key for the supplied key code and modifer flags then nil is returned.
@param keyCode The key code used by the keyboard, can vary across hardware.
@param aChar The character, used for display purposes only.
@param modifierFlags The modifer flags, ( <tt>NSCommandKeyMask</tt>, <tt>NSControlKeyMask</tt>, <tt>NSAlternateKeyMask</tt>, <tt>NSShiftKeyMask</tt> ).
@result An new <tt>NDHotKeyEvent</tt> or nil if failure.
*/
+ (id)hotKeyWithKeyCode:(unsigned short)keyCode character:(unichar)aChar modifierFlags:(unsigned int)modifer;
/*!
@method hotKeyWithKeyCode:character:modifierFlags:target:selector:
@abstract Get a <tt>NDHotKeyEvent</tt> object.
@discussion Returns a new hot key for the supplied hot key combination and target object and selector, if there is already a hot key for the supplied key code and modifer flags then nil is returned.
@param keyCode The key code used by the keyboard, can vary across hardware.
@param aChar The character, used for display purposes only.
@param modifierFlags The modifer flags, ( <tt>NSCommandKeyMask</tt>, <tt>NSControlKeyMask</tt>, <tt>NSAlternateKeyMask</tt>, <tt>NSShiftKeyMask</tt> ).
@param target The target of hot key event.
@param selector The selector sent when hot key is released
@result A new <tt>NDHotKeyEvent</tt>
*/
+ (id)hotKeyWithKeyCode:(unsigned short)keyCode character:(unichar)aChar modifierFlags:(unsigned int)modifer target:(id)target selector:(SEL)selector;
/*!
@method initWithPropertyList:
@abstract creates a <tt>NDHotKeyEvent</tt> with a property list.
@discussion This can be used for archiving purposes, but it is possible that it will not work if the users keyboard is changed, ie between machines.
@param propertyList A property list object
@result A initialized <tt>NDHotKeyEvent</tt>
*/
+ (id)hotKeyWithWithPropertyList:(id)propertyList;
/*!
@method initWithKeyCode:character:modifierFlags:target:selector:
@abstract Initialize a <tt>NDHotKeyEvent</tt> object.
@discussion Initialize the reciever with the supplied hot key combination and target object and selector, if there is already a hot key for the supplied key code and modifer flags then nil is returned.
@param keyCode The key code used by the keyboard, can vary across hardware.
@param aChar The character, used for display purposes only.
@param modifierFlags The modifer flags, ( <tt>NSCommandKeyMask</tt>, <tt>NSControlKeyMask</tt>, <tt>NSAlternateKeyMask</tt>, <tt>NSShiftKeyMask</tt> ).
@param target The target of hot key event.
@param selector The selector sent when hot key is released
@result A initialized <tt>NDHotKeyEvent</tt>
*/
- (id)initWithKeyCode:(unsigned short)keyCode character:(unichar)aChar modifierFlags:(unsigned int)modifer target:(id)target selector:(SEL)selector;
/*!
@method initWithKeyCode:character:modifierFlags
@abstract Initialize a <tt>NDHotKeyEvent</tt> object.
@discussion Initialize the reciever with the supplied hot key combination, if there is already a hot key for the supplied key code and modifer flags then nil is returned.
@param keyCode The key code used by the keyboard, can vary across hardware.
@param aChar The character, used for display purposes only.
@param modifierFlags The modifer flags, ( <tt>NSCommandKeyMask</tt>, <tt>NSControlKeyMask</tt>, <tt>NSAlternateKeyMask</tt>, <tt>NSShiftKeyMask</tt> ).
@result A initialized <tt>NDHotKeyEvent</tt>
*/
- (id)initWithKeyCode:(unsigned short)keyCode character:(unichar)aChar modifierFlags:(unsigned int)modifer;
/*!
@method initWithPropertyList:
@abstract Initializes the reciever with a property list.
@discussion This can be used for archiving purposes, but it is possible that it will not work if the users keyboard is changed, ie between machines. The following properties are initialised
<ul>
<li>Key Code</li>
<li>Character</li>
<li>Modifier Flags</li>
<li>Selector Pressed</li>
<li>Selector Released</li>
</ul>
@param propertyList A property list object
@result A initialized <tt>NDHotKeyEvent</tt>
*/
- (id)initWithPropertyList:(id)propertyList;
/*!
@method propertyList
@abstract Returns a property list for the reciever.
@discussion This can be used for archiving purposes, but it is possible that it will not work if the users keyboard is changed, ie between machines. The property list returned contains the following properties;
<ul>
<li>Key Code</li>
<li>Character</li>
<li>Modifier Flags</li>
<li>Selector Pressed</li>
<li>Selector Released</li>
</ul>
@result The property list object.
*/
- (id)propertyList;
/*!
@method initWithCoder:
@abstract Initializes a newly allocated instance from data in <tt>decoder</tt>.
@discussion Decodes the following properties of a <tt>NDHotKeyEvent</tt>;
<ul>
<li>Key Code</li>
<li>Character</li>
<li>Modifier Flags</li>
<li>Selector Pressed</li>
<li>Selector Released</li>
</ul>
Will use Keyed Coding if <code>[<i>decoder</i> allowsKeyedCoding] == YES</code>.
@param decoder A subclass of <tt>NSCoder</tt>
@result A initialized <tt>NDHotKeyEvent</tt>
*/
- (id)initWithCoder:(NSCoder *)decoder;
/*!
@method encodeWithCoder:
@abstract Encodes the receiver using <tt>encoder</tt>
@discussion Encodes the following properties of a <tt>NDHotKeyEvent</tt>;
<ul>
<li>Key Code</li>
<li>Character</li>
<li>Modifier Flags</li>
<li>Selector Pressed</li>
<li>Selector Released</li>
</ul>
Will use Keyed Coding if <code>[<i>encoder</i> allowsKeyedCoding] == YES</code>.
@param encoder A subclass of <tt>NSCoder</tt>.
*/
- (void)encodeWithCoder:(NSCoder *)encoder;
/*!
@method setEnabled:
@abstract Set the hot key enabled or disable.
@discussion <tt>setEnabled:</tt> registers or unregisters the recievers hot key combination.
@param flag <tt>YES</tt> to enable, <tt>NO</tt> to disable.
@result Returns <tt>YES</tt> if successful
*/
- (BOOL)setEnabled:(BOOL)flag;
/*!
@method isEnabled
@abstract Find out if a hot key is enabled.
@discussion Returns <tt>YES</tt> if the hot key is registered.
@result <tt>YES</tt> if enabled.
*/
- (BOOL)isEnabled;
/*!
@method target
@abstract Get the hot key event target.
@discussion Returns the object that is sent the key pressed and key released hot key events, see the methods <tt>-selector</tt>, <tt>-selectorReleased</tt> and <tt>selectorPressed</tt>.
@result The target object.
*/
- (id)target;
/*!
@method selector
@abstract The selector for a key released event.
@discussion This is the selector sent when the hot key combination for the reciever is released. This is the same selector has returned from the method <tt>[NDHotKeyEvent selectorReleased]</tt>
@result The method selector.
*/
- (SEL)selector;
/*!
@method selectorReleased
@abstract The selector for a key released event.
@discussion This is the selector sent when the hot key combination for the reciever is released. This is the same selector has returned from the method <tt>[NDHotKeyEvent selector]</tt>
@result The method selector.
*/
- (SEL)selectorReleased;
/*!
@method selectorPressed
@abstract The selector for a key pressed event.
@discussion This is the selector sent when the hot key combination for the reciever is pressed.
@result The method selector.
*/
- (SEL)selectorPressed;
/*!
@method currentEventType
@abstract Get the current hot key event type.
@discussion This value returns what event last occured. Can be used in your target when it is sent a event message to find out what event occured, possible values are
<blockquote>
<table border = "1" width = "90%">
<thead><tr><th>Value</th><th>Description</th></tr></thead>
<tr><td align = "center"><tt>NDHotKeyNoEvent</tt></td><td>The hot key has not been pressed yet.</td></tr>
<tr><td align = "center"><tt>NDHotKeyPressedEvent</tt></td><td>The hot key was pressed last.</td></tr>
<tr><td align = "center"><tt>NDHotKeyReleasedEvent</tt></td><td>The hot key was released last.</td></tr>
</table>
</blockquote>
@result The last event type.
*/
- (int)currentEventType;
/*!
@method setTarget:selector:
@abstract Set the hot key target.
@discussion Set the target object and selector to be sent when the hot key is released.
@param target The traget object.
@param selector The selector.
@result returns <tt>YES</tt> if successful.
*/
- (BOOL)setTarget:(id)target selector:(SEL)selector;
/*!
@method setTarget:selectorReleased:selectorPressed:
@abstract Set the hot key target.
@discussion Set the target object and selector to be sent when the hot key is pressed and wehn it is released.
@param target The traget object.
@param selectorReleased The key released selector.
@param selectorPressed The key pressed selector.
@result returns <tt>YES</tt> if successful.
*/
- (BOOL)setTarget:(id)target selectorReleased:(SEL)selectorReleased selectorPressed:(SEL)selectorPressed;
/*!
@method performHotKeyReleased
@abstract Invoke the target with the release selector.
@discussion Use to send the selector for a release event, though this method can be called by you.
*/
- (void)performHotKeyReleased;
/*!
@method performHotKeyPressed
@abstract Invoke the target with the press selector.
@discussion Use to send the selector for a presse event, though this method can be called by you.
*/
- (void)performHotKeyPressed;
/*!
@method keyCode
@abstract Get the hot key key code.
@discussion The key code for the hot key, this is hardware specific.
@result The key code.
*/
- (unsigned short)keyCode;
/*!
@method character
@abstract Get the hot key character.
@discussion This is the character for the key code, without modifier keys. The character is for display purposes only and dose not determine the key code.
@result A uni code character.
*/
- (unichar)character;
/*!
@method modifierFlags
@abstract Get the hot key modifer key flags.
@discussion The <tt>modifierFlags</tt> can be a bitwise and combination of <tt>NSControlKeyMask</tt>, <tt>NSAlternateKeyMask</tt>, <tt>NSShiftKeyMask</tt>, and <tt>NSCommandKeyMask</tt>.
@result The modifer key flags.
*/
- (unsigned int)modifierFlags;
/*!
@method stringValue
@abstract Get a string got the hot keys.
@discussion This is a string that can be used for display purposes.
@result A <tt>NSString</tt>
*/
- (NSString *)stringValue;
@end
/*!
@protocol NSObject(NDHotKeyEventTragetWillChange)
@abstract Informal protocol used to inform a <tt>NDHotKeyEvent</tt> target of events.
@discussion The informal protocol <tt>NDHotKeyEventTragetWillChange</tt> defines a method used to notify a <tt>NDHotKeyEvent</tt> target that the target will change.
*/
@interface NSObject (NDHotKeyEventTragetWillChange)
/*!
@method targetWillChangeToObject:forHotKeyEvent:
@abstract Message sent to a target object to inform it that the target is going to change.
@discussion This method can be used to notify the receiver that it will no longer be the target for a <tt>NDHotKeyEvent</tt> or used to prevent the target from changing by returning <tt>NO</tt>
@param target The new target for the <tt>NDHotKeyEvent</tt>
@param hotKeyEvent The <tt>NDHotKeyEvent</tt> for which the target is changing.
@result Return <tt>NO</tt> to prevent the target from changing, otherwise return <tt>YES</tt>.
*/
- (BOOL)targetWillChangeToObject:(id)target forHotKeyEvent:(NDHotKeyEvent *)hotKeyEvent;
@end
/*!
@function stringForKeyCodeAndModifierFlags
@abstract Get a string for hot key parameters.
@discussion Returns a string representation of the passed in hot key values.
@param keyCode A key code.
@param aChar A character representation of the key code.
@param modifierFlags modifer flags, comman, option, shift and control.
@result A <tt>NSString</tt> representing the hot key combination.
*/
NSString * stringForKeyCodeAndModifierFlags( unsigned short keyCode, unichar aChar, unsigned int modifierFlags );
/*!
@function unicharForKeyCode
@abstract Get a unicode charater for the key combination.
@discussion The uncode chararter for the key combination.
@param keyCode The key code used by the keyboard, can vary across hardware.
@result A <tt>unichar</tt>
*/
unichar unicharForKeyCode( unsigned short keyCode );