监听全局事件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyUpMask 
handler:^(NSEvent * _Nonnull event) {
[self handleEvent:event];
}];

-(BOOL) handleEvent:(NSEvent *) event{
const NSUInteger kNotAlt = NSAlternateKeyMask | NSShiftKeyMask | NSCommandKeyMask;
if ((event.modifierFlags & NSControlKeyMask) != 0
&&
((event.modifierFlags & kNotAlt) == 0)
&&
[event.charactersIgnoringModifiers isEqualToString:@"x"]) {
.....

// 激活app
[NSApp activateIgnoringOtherApps:YES];
return YES;
}
return NO;
}