forked from ggerganov/kbd-audio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_logger.cpp
185 lines (166 loc) · 5.53 KB
/
key_logger.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*! \file key_logger.cpp
* \brief Enter description here.
* \author Georgi Gerganov
*/
#include "key_logger.h"
#include <ApplicationServices/ApplicationServices.h>
#include <thread>
namespace {
CGEventRef CGEventCallback(CGEventTapProxy proxy, CGEventType type, CGEventRef event, void * userData) {
if (type != kCGEventKeyDown && type != kCGEventFlagsChanged && type != kCGEventKeyUp) {
return event;
}
CGKeyCode keyCode = (CGKeyCode) CGEventGetIntegerValueField(event, kCGKeyboardEventKeycode);
KeyLogger * data = static_cast<KeyLogger *>(userData);
data->getCallback()(keyCode);
return event;
}
}
struct KeyLogger::Data {
Callback callback;
std::thread worker;
CFRunLoopRef runLoopRef;
};
KeyLogger::KeyLogger() : data_(new Data()) {}
KeyLogger::~KeyLogger() {
CFRunLoopStop(data_->runLoopRef);
data_->worker.join();
}
bool KeyLogger::install(std::function<void(int)> callback) {
data_->callback = std::move(callback);
data_->worker = std::thread([this]() {
CGEventMask eventMask = (CGEventMaskBit(kCGEventKeyDown) | CGEventMaskBit(kCGEventFlagsChanged));
CFMachPortRef eventTap = CGEventTapCreate(
kCGSessionEventTap, kCGHeadInsertEventTap, (CGEventTapOptions) 0, eventMask, CGEventCallback, this);
if (!eventTap) {
return;
}
CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CGEventTapEnable(eventTap, true);
this->data_->runLoopRef = CFRunLoopGetCurrent();
CFRunLoopRun();
});
return true;
}
const KeyLogger::Callback & KeyLogger::getCallback() const {
return data_->callback;
}
const char * KeyLogger::codeToText(const Code & code) {
switch ((int) code) {
case 0: return "a";
case 1: return "s";
case 2: return "d";
case 3: return "f";
case 4: return "h";
case 5: return "g";
case 6: return "z";
case 7: return "x";
case 8: return "c";
case 9: return "v";
case 11: return "b";
case 12: return "q";
case 13: return "w";
case 14: return "e";
case 15: return "r";
case 16: return "y";
case 17: return "t";
case 18: return "1";
case 19: return "2";
case 20: return "3";
case 21: return "4";
case 22: return "6";
case 23: return "5";
case 24: return "=";
case 25: return "9";
case 26: return "7";
case 27: return "-";
case 28: return "8";
case 29: return "0";
case 30: return "]";
case 31: return "o";
case 32: return "u";
case 33: return "[";
case 34: return "i";
case 35: return "p";
case 37: return "l";
case 38: return "j";
case 39: return "'";
case 40: return "k";
case 41: return ";";
case 42: return "\\";
case 43: return ",";
case 44: return "/";
case 45: return "n";
case 46: return "m";
case 47: return ".";
case 50: return "`";
case 65: return "[decimal]";
case 67: return "[asterisk]";
case 69: return "[plus]";
case 71: return "[clear]";
case 75: return "[divide]";
case 76: return "[enter]";
case 78: return "[hyphen]";
case 81: return "[equals]";
case 82: return "0";
case 83: return "1";
case 84: return "2";
case 85: return "3";
case 86: return "4";
case 87: return "5";
case 88: return "6";
case 89: return "7";
case 91: return "8";
case 92: return "9";
case 36: return "[return]";
case 48: return "[tab]";
case 49: return " ";
case 51: return "[del]";
case 53: return "[esc]";
case 54: return "[right-cmd]";
case 55: return "[left-cmd]";
case 56: return "[left-shift]";
case 57: return "[caps]";
case 58: return "[left-option]";
case 59: return "[left-ctrl]";
case 60: return "[right-shift]";
case 61: return "[right-option]";
case 62: return "[right-ctrl]";
case 63: return "[fn]";
case 64: return "[f17]";
case 72: return "[volup]";
case 73: return "[voldown]";
case 74: return "[mute]";
case 79: return "[f18]";
case 80: return "[f19]";
case 90: return "[f20]";
case 96: return "[f5]";
case 97: return "[f6]";
case 98: return "[f7]";
case 99: return "[f3]";
case 100: return "[f8]";
case 101: return "[f9]";
case 103: return "[f11]";
case 105: return "[f13]";
case 106: return "[f16]";
case 107: return "[f14]";
case 109: return "[f10]";
case 111: return "[f12]";
case 113: return "[f15]";
case 114: return "[help]";
case 115: return "[home]";
case 116: return "[pgup]";
case 117: return "[fwddel]";
case 118: return "[f4]";
case 119: return "[end]";
case 120: return "[f2]";
case 121: return "[pgdown]";
case 122: return "[f1]";
case 123: return "[left]";
case 124: return "[right]";
case 125: return "[down]";
case 126: return "[up]";
}
return "[unknown]";
}