Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Mabdelwanis/26825198271b264892d9e601b5f3b659 to your computer and use it in GitHub Desktop.
Save Mabdelwanis/26825198271b264892d9e601b5f3b659 to your computer and use it in GitHub Desktop.
#include "keylogger.h"
int main(int argc, const char *argv[]) {
// Create an event tap to retrieve keypresses.
CGEventMask eventMask = (CGEventMaskBit(kCGEventKeyDown) | CGEventMaskBit(kCGEventFlagsChanged));
CFMachPortRef eventTap = CGEventTapCreate(
kCGSessionEventTap, kCGHeadInsertEventTap, 0, eventMask, CGEventCallback, NULL
);
// Exit the program if unable to create the event tap.
if(!eventTap) {
fprintf(stderr, "ERROR: Unable to create event tap.\n");
exit(1);
}
// Create a run loop source and add enable the event tap.
CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
CFRunLoopRun();
return 0;
}
static char * previousKey;
// The following callback method is invoked on every keypress.
CGEventRef CGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
if (type != kCGEventKeyDown && type != kCGEventFlagsChanged && type != kCGEventKeyUp) { return event; }
// Retrieve the incoming keycode.
CGKeyCode keyCode = (CGKeyCode) CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode);
char * currentKey = convertKeyCode(keyCode);
char * target = ";";
if( currentKey == previousKey && currentKey == target ){
system("echo \"tell application \\\"System Events\\\" to keystroke \\\"S\\\" using {command down, shift down}\" | osascript");
previousKey = NULL;
}
previousKey = currentKey;
return event;
}
// The following method converts the key code returned by each keypress as
// a human readable key code in const char format.
char *convertKeyCode(int keyCode) {
switch ((int) keyCode) {
case 41: return ";";
}
return "[unknown]";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment