forked from davedelong/DDHotKey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DDHotKeyCenter.h
96 lines (70 loc) · 3.49 KB
/
DDHotKeyCenter.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
/*
DDHotKey -- DDHotKeyCenter.h
Copyright (c) Dave DeLong <http://www.davedelong.com>
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.
The software is provided "as is", without warranty of any kind, including all implied warranties of merchantability and fitness. In no event shall the author(s) or copyright holder(s) be liable for any claim, damages, or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software.
*/
#import <Cocoa/Cocoa.h>
NS_ASSUME_NONNULL_BEGIN
//a convenient typedef for the required signature of a hotkey block callback
typedef void (^DDHotKeyTask)(NSEvent*);
@interface DDHotKey : NSObject
// creates a new hotkey but does not register it
+ (instancetype)hotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags task:(DDHotKeyTask _Nullable)task;
@property (nonatomic, assign, readonly, nullable) id target;
@property (nonatomic, readonly, nullable) SEL action;
@property (nonatomic, strong, readonly, nullable) id object;
@property (nonatomic, copy, readonly, nullable) DDHotKeyTask task;
@property (nonatomic, readonly) unsigned short keyCode;
@property (nonatomic, readonly) NSUInteger modifierFlags;
@end
#pragma mark -
@interface DDHotKeyCenter : NSObject
@property (class, readonly, nonnull) DDHotKeyCenter *sharedHotKeyCenter;
/**
Register a hotkey.
*/
- (DDHotKey * _Nullable)registerHotKey:(DDHotKey *)hotKey withError:(NSError **)error;
/**
Register a target/action hotkey.
The modifierFlags must be a bitwise OR of NSEventModifierFlagCommand, NSEventModifierFlagOption, NSEventModifierFlagControl, or NSEventModifierFlagShift;
Returns the hotkey registered. If registration failed, returns nil.
*/
- (DDHotKey * _Nullable)registerHotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags target:(id)target action:(SEL)action object:(id)object error:(NSError **)error;
/**
Register a block callback hotkey.
The modifierFlags must be a bitwise OR of NSEventModifierFlagCommand, NSEventModifierFlagOption, NSEventModifierFlagControl, or NSEventModifierFlagShift;
Returns the hotkey registered. If registration failed, returns nil.
*/
- (DDHotKey * _Nullable)registerHotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags task:(DDHotKeyTask)task error:(NSError **)error;
/**
See if a hotkey exists with the specified keycode and modifier flags.
NOTE: this will only check among hotkeys you have explicitly registered with DDHotKeyCenter. This does not check all globally registered hotkeys.
*/
- (BOOL)hasRegisteredHotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags;
/**
Unregister a specific hotkey
*/
- (void)unregisterHotKey:(DDHotKey *)hotKey;
/**
Unregister all hotkeys
*/
- (void)unregisterAllHotKeys;
/**
Unregister all hotkeys with a specific target
*/
- (void)unregisterHotKeysWithTarget:(id)target;
/**
Unregister all hotkeys with a specific target and action
*/
- (void)unregisterHotKeysWithTarget:(id)target action:(SEL)action;
/**
Unregister a hotkey with a specific keycode and modifier flags
*/
- (void)unregisterHotKeyWithKeyCode:(unsigned short)keyCode modifierFlags:(NSUInteger)flags;
/**
Returns a set of currently registered hotkeys
**/
- (NSSet<DDHotKey *> *)registeredHotKeys;
@end
NS_ASSUME_NONNULL_END