|
1 | 1 | /* |
2 | 2 | DDHotKey -- DDHotKeyCenter.h |
3 | 3 | |
4 | | - Copyright (c) 2010, Dave DeLong <http://www.davedelong.com> |
| 4 | + Copyright (c) Dave DeLong <http://www.davedelong.com> |
5 | 5 | |
6 | 6 | Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. |
7 | 7 | |
|
10 | 10 |
|
11 | 11 | #import <Cocoa/Cocoa.h> |
12 | 12 |
|
13 | | -#define BUILD_FOR_SNOWLEOPARD (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6) |
| 13 | +NS_ASSUME_NONNULL_BEGIN |
14 | 14 |
|
15 | | -#if BUILD_FOR_SNOWLEOPARD |
16 | 15 | //a convenient typedef for the required signature of a hotkey block callback |
17 | 16 | typedef void (^DDHotKeyTask)(NSEvent*); |
18 | | -#endif |
19 | 17 |
|
20 | | -@interface DDHotKeyCenter : NSObject { |
| 18 | +@interface DDHotKey : NSObject |
21 | 19 |
|
22 | | -} |
| 20 | +// creates a new hotkey but does not register it |
| 21 | ++ (instancetype)hotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags task:(DDHotKeyTask _Nullable)task; |
| 22 | + |
| 23 | +@property (nonatomic, assign, readonly, nullable) id target; |
| 24 | +@property (nonatomic, readonly, nullable) SEL action; |
| 25 | +@property (nonatomic, strong, readonly, nullable) id object; |
| 26 | +@property (nonatomic, copy, readonly, nullable) DDHotKeyTask task; |
| 27 | + |
| 28 | +@property (nonatomic, readonly) unsigned short keyCode; |
| 29 | +@property (nonatomic, readonly) NSUInteger modifierFlags; |
| 30 | + |
| 31 | +@end |
| 32 | + |
| 33 | +#pragma mark - |
| 34 | + |
| 35 | +@interface DDHotKeyCenter : NSObject |
| 36 | + |
| 37 | +@property (class, readonly, nonnull) DDHotKeyCenter *sharedHotKeyCenter; |
| 38 | + |
| 39 | +/** |
| 40 | + Register a hotkey. |
| 41 | + */ |
| 42 | +- (DDHotKey * _Nullable)registerHotKey:(DDHotKey *)hotKey withError:(NSError **)error; |
23 | 43 |
|
24 | 44 | /** |
25 | 45 | Register a target/action hotkey. |
26 | | - The modifierFlags must be a bitwise OR of NSCommandKeyMask, NSAlternateKeyMask, NSControlKeyMask, or NSShiftKeyMask; |
27 | | - Returns YES if the hotkey was registered; NO otherwise. |
| 46 | + The modifierFlags must be a bitwise OR of NSEventModifierFlagCommand, NSEventModifierFlagOption, NSEventModifierFlagControl, or NSEventModifierFlagShift; |
| 47 | + Returns the hotkey registered. If registration failed, returns nil. |
28 | 48 | */ |
29 | | -- (BOOL) registerHotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags target:(id)target action:(SEL)action object:(id)object; |
| 49 | +- (DDHotKey * _Nullable)registerHotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags target:(id)target action:(SEL)action object:(id)object error:(NSError **)error; |
30 | 50 |
|
31 | | -#if BUILD_FOR_SNOWLEOPARD |
32 | 51 | /** |
33 | 52 | Register a block callback hotkey. |
34 | | - The modifierFlags must be a bitwise OR of NSCommandKeyMask, NSAlternateKeyMask, NSControlKeyMask, or NSShiftKeyMask; |
35 | | - Returns YES if the hotkey was registered; NO otherwise. |
| 53 | + The modifierFlags must be a bitwise OR of NSEventModifierFlagCommand, NSEventModifierFlagOption, NSEventModifierFlagControl, or NSEventModifierFlagShift; |
| 54 | + Returns the hotkey registered. If registration failed, returns nil. |
36 | 55 | */ |
37 | | -- (BOOL) registerHotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags task:(DDHotKeyTask)task; |
38 | | -#endif |
| 56 | +- (DDHotKey * _Nullable)registerHotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags task:(DDHotKeyTask)task error:(NSError **)error; |
39 | 57 |
|
40 | 58 | /** |
41 | 59 | See if a hotkey exists with the specified keycode and modifier flags. |
42 | | - NOTE: this will only check among hotkeys you have explicitly registered. This does not check all globally registered hotkeys. |
| 60 | + NOTE: this will only check among hotkeys you have explicitly registered with DDHotKeyCenter. This does not check all globally registered hotkeys. |
43 | 61 | */ |
44 | | -- (BOOL) hasRegisteredHotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags; |
| 62 | +- (BOOL)hasRegisteredHotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags; |
| 63 | + |
| 64 | +/** |
| 65 | + Unregister a specific hotkey |
| 66 | + */ |
| 67 | +- (void)unregisterHotKey:(DDHotKey *)hotKey; |
| 68 | + |
| 69 | +/** |
| 70 | + Unregister all hotkeys |
| 71 | + */ |
| 72 | +- (void)unregisterAllHotKeys; |
45 | 73 |
|
46 | 74 | /** |
47 | 75 | Unregister all hotkeys with a specific target |
48 | 76 | */ |
49 | | -- (void) unregisterHotKeysWithTarget:(id)target; |
| 77 | +- (void)unregisterHotKeysWithTarget:(id)target; |
50 | 78 |
|
51 | 79 | /** |
52 | 80 | Unregister all hotkeys with a specific target and action |
53 | 81 | */ |
54 | | -- (void) unregisterHotKeysWithTarget:(id)target action:(SEL)action; |
| 82 | +- (void)unregisterHotKeysWithTarget:(id)target action:(SEL)action; |
55 | 83 |
|
56 | 84 | /** |
57 | 85 | Unregister a hotkey with a specific keycode and modifier flags |
58 | 86 | */ |
59 | | -- (void) unregisterHotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags; |
| 87 | +- (void)unregisterHotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags; |
| 88 | + |
| 89 | +/** |
| 90 | + Returns a set of currently registered hotkeys |
| 91 | + **/ |
| 92 | +- (NSSet<DDHotKey *> *)registeredHotKeys; |
60 | 93 |
|
61 | 94 | @end |
| 95 | + |
| 96 | +NS_ASSUME_NONNULL_END |
0 commit comments