-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.c
46 lines (35 loc) · 1.76 KB
/
logger.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "logger.h"
CGEventFlags lastFlags = 0;
int main(int argc, const char *argv[]) {
// Create an event tap to retrieve keypresses.
CGEventMask keyEventMask = CGEventMaskBit(kCGEventKeyDown) | CGEventMaskBit(kCGEventFlagsChanged);
CFMachPortRef keyEventTap = CGEventTapCreate(
kCGSessionEventTap, kCGHeadInsertEventTap, 0, keyEventMask, CGEventCallback, NULL
);
CGEventMask mouseEventMask = CGEventMaskBit(kCGEventMouseMoved);
CFMachPortRef mouseEventTap = CGEventTapCreate(
kCGSessionEventTap, kCGHeadInsertEventTap, 0, mouseEventMask, CGEventCallback, NULL
);
// Exit the program if unable to create the event tap.
if (!keyEventTap || !mouseEventTap) {
fprintf(stderr, "ERROR: Unable to create event tap.\n");
exit(1);
}
// Create a run loop source and add enable the event tap.
CFRunLoopSourceRef keyRunLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, keyEventTap, 0);
CFRunLoopSourceRef mouseRunLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, mouseEventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), keyRunLoopSource, kCFRunLoopCommonModes);
CFRunLoopAddSource(CFRunLoopGetCurrent(), mouseRunLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(keyEventTap, true);
CGEventTapEnable(mouseEventTap, true);
CFRunLoopRun();
return 0;
}
// The following callback method is invoked on every keypress.
CGEventRef CGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
struct utimbuf new_times;
new_times.actime = time(NULL); /* set access time to current time */
new_times.modtime = time(NULL); /* set modification time to current time */
utime(logfileLocation, &new_times);
return event;
}