なんか作業が捗らないので見える化のためにキーロガーを探してたらよさ気なやつを見つけたので改造.
61503891さんの Cで作るOSXのキーロガー を参考にさせて頂きました.
元のやつだとキー入力がある度に入力キーが出力されてたので10分ごとに押された数をカウントするように修正.
ついでにマウスのクリックもカウント(左クリックだけ).
最後の10分間が出力されないけど気にしない.
コード
keylogger.c
// This program is a simple implementation of a key logger for OS X.
// This key loggger requires sudo and outputs key codes and a time stamp to standard output.
//
// build: cc keylogger.c -o keylogger -framework ApplicationServices
// usage: sudo keylogger > keylogger.log
// author: Masayuki Higashino
# include <stdio.h>
# include <time.h>
# include <ApplicationServices/ApplicationServices.h>
# define INTERVAL 600
CGEventRef on_tap(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void *refcon) {
//CGKeyCode key = CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode);
static int count = 0;
static int mouse_count = 0;
static time_t next_time = 0;
time_t now; time(&now);
if (next_time == 0) {
next_time = ((int)now / INTERVAL) * INTERVAL + INTERVAL;
}
if (type == kCGEventKeyDown) {
count++;
} else {
mouse_count++;
}
if (next_time <= now) {
printf("%d - %d %d %d\n", (int)next_time - INTERVAL, (int)next_time, count, mouse_count); fflush(stdout);
next_time = ((int)now / INTERVAL) * INTERVAL + INTERVAL;
count = 0;
mouse_count = 0;
}
return event;
}
int main(int argc, const char * argv[]) {
CGEventFlags flags = CGEventSourceFlagsState(kCGEventSourceStateCombinedSessionState);
//CGEventMask mask = CGEventMaskBit(kCGEventKeyDown);
CGEventMask mask = CGEventMaskBit(kCGEventKeyDown) | CGEventMaskBit(kCGEventLeftMouseDown);
CFMachPortRef tap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0, mask, on_tap, &flags);
if (!tap) {
fprintf(stderr, "This program requires sudo.");
return -1;
}
CFRunLoopSourceRef runloop = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, tap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runloop, kCFRunLoopCommonModes);
CGEventTapEnable(tap, true);
CFRunLoopRun();
return 0;
}
コンパイル
$ cc keylogger.c -o keylogger -framework ApplicationServices
実行
$ sudo keylogger > keylogger.log
実行結果
1435589400 - 1435590000 443 18
1435590000 - 1435590600 473 31
# 開始時刻 - 終了時刻 キー入力数 マウス左クリック数