-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathHotKey.m
More file actions
112 lines (92 loc) · 3.15 KB
/
HotKey.m
File metadata and controls
112 lines (92 loc) · 3.15 KB
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
#import "HotKey.h"
#import "KeyCodeConverter.h"
#import <Carbon/Carbon.h>
@interface HotKey ()
@property (nonatomic, copy, readwrite) NSString *key;
@property (nonatomic, strong, readwrite) NSDictionary *mapping;
@property (nonatomic, readwrite) int keyCode;
@property (nonatomic, readwrite) int modifiers;
@property (nonatomic, readwrite) int carbonModifiers;
@end
@implementation HotKey
- (id)initWithKey:(NSString *)key mapping:(NSDictionary *)mapping {
self = [super init];
if (self) {
_key = key;
_mapping = mapping;
[self setup];
}
return self;
}
- (void)setup {
[self parseKey];
}
- (void)parseKey {
NSDictionary *fixKeys = [KeyCodeConverter fixKeys];
NSArray *components = [self.key componentsSeparatedByString:@" "];
int modifiers = 0;
int carbonModifiers = 0;
for (NSString *component in components) {
int keyCode = [fixKeys[component] intValue];
switch (keyCode) {
case kVK_Shift:
modifiers |= kCGEventFlagMaskShift;
carbonModifiers |= shiftKey;
break;
case kVK_Control:
modifiers |= kCGEventFlagMaskControl;
carbonModifiers |= controlKey;
break;
case kVK_Option:
modifiers |= kCGEventFlagMaskAlternate;
carbonModifiers |= optionKey;
break;
case kVK_Command:
modifiers |= kCGEventFlagMaskCommand;
carbonModifiers |= cmdKey;
break;
default:
if (fixKeys[component]) {
self.keyCode = keyCode;
} else {
self.keyCode = [KeyCodeConverter toKeyCode:component];
}
break;
}
}
self.modifiers = modifiers;
self.carbonModifiers = carbonModifiers;
}
- (NSArray *)mappedHotKeysForAppWithName:(NSString *)appName {
NSString *mapping = self.mapping[appName];
NSArray *mappingComponents = [mapping componentsSeparatedByString:@"|"];
NSMutableArray *mappedKeys = [NSMutableArray array];
for (NSString *mappingComponent in mappingComponents) {
NSString *trimmedComponent = [mappingComponent stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceCharacterSet]];
HotKey *hotKey = [[HotKey alloc] initWithKey:trimmedComponent
mapping:nil];
[mappedKeys addObject:hotKey];
}
return mappedKeys;
}
- (NSString *)description {
return [NSString stringWithFormat:@"key: %@, keyCode: %d, modifiers: %d, mapping: %@",
self.key, self.keyCode, self.modifiers, self.mapping];
}
- (NSUInteger)hash {
return self.keyCode ^ self.modifiers;
}
- (BOOL)isEqual:(id)object {
if (object == self) {
return YES;
}
BOOL equal = NO;
if ([object isKindOfClass:[HotKey class]]) {
HotKey *hotKey = (HotKey *)object;
equal = (hotKey.keyCode == self.keyCode);
equal &= (hotKey.modifiers == self.modifiers);
}
return equal;
}
@end