Skip to content

Instantly share code, notes, and snippets.

@thomcc

thomcc/events.m Secret

Created September 2, 2016 16:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomcc/6f41cd2043130319c8dc3af4e8701ee4 to your computer and use it in GitHub Desktop.
Save thomcc/6f41cd2043130319c8dc3af4e8701ee4 to your computer and use it in GitHub Desktop.
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[NSEvent addLocalMonitorForEventsMatchingMask: (NSKeyDownMask | NSKeyUpMask | NSFlagsChangedMask)
handler: ^(NSEvent *event) {
NSEvent *result = event;
int flags = [event modifierFlags];
int cmdDown = !!(flags & NSCommandKeyMask);
int shiftDown = !!(flags & NSShiftKeyMask);
int ctrlDown = !!(flags & NSControlKeyMask);
int altDown = !!(flags & NSAlternateKeyMask);
switch ([event type]) {
case NSKeyDown: {
unichar c = [[event charactersIgnoringModifiers] characterAtIndex:0];
printf("KEYDOWN Key: %d, Cmd: %d, Shift: %d, Ctrl: %d, Alt: %d\n", c, cmdDown, shiftDown, ctrlDown, altDown);
result = nil;
} break;
case NSKeyUp: {
unichar c = [[event charactersIgnoringModifiers] characterAtIndex:0];
printf("KEYUP Key: %d, Cmd: %d, Shift: %d, Ctrl: %d, Alt: %d\n", c, cmdDown, shiftDown, ctrlDown, altDown);
result = nil;
} break;
case NSFlagsChanged: {
printf("FLAGS Cmd: %d, Shift: %d, Ctrl: %d, Alt: %d\n", cmdDown, shiftDown, ctrlDown, altDown);
} break;
default:
break;
}
return result;
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment