-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMouseLogger.c
More file actions
207 lines (174 loc) · 4.52 KB
/
MouseLogger.c
File metadata and controls
207 lines (174 loc) · 4.52 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#ifndef UNICODE
#define UNICODE
#endif
#include <Windows.h>
#include <Windowsx.h>
#include <stdio.h>
#include <limits.h>
#ifndef HID_USAGE_PAGE_GENERIC
#define HID_USAGE_PAGE_GENERIC ((USHORT) 0x01)
#endif
#ifndef HID_USAGE_GENERIC_MOUSE
#define HID_USAGE_GENERIC_MOUSE ((USHORT) 0x02)
#endif
typedef struct {
int count;
int total;
int minimum;
int maximum;
} Stats;
BOOL motionDetected;
int lastX;
int lastY;
Stats hStats, vStats;
Stats hRawStats, vRawStats;
char buffer1[128];
char buffer2[128];
void reset(Stats *stats)
{
stats->count = 0;
stats->total = 0;
stats->minimum = INT_MAX;
stats->maximum = 0;
}
void resetStats()
{
reset(&hStats);
reset(&vStats);
reset(&hRawStats);
reset(&vRawStats);
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
const wchar_t CLASS_NAME[] = L"Window Class";
WNDCLASS wc = {};
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
RegisterClass(&wc);
HWND hWnd = CreateWindowEx(
0, CLASS_NAME, L"Mouse Logger",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
NULL, NULL, hInstance, NULL);
RAWINPUTDEVICE devices[1];
devices[0].usUsagePage = HID_USAGE_PAGE_GENERIC;
devices[0].usUsage = HID_USAGE_GENERIC_MOUSE;
devices[0].dwFlags = RIDEV_INPUTSINK;
devices[0].hwndTarget = hWnd;
RegisterRawInputDevices(devices, 1, sizeof(devices[0]));
RECT rc;
GetWindowRect(hWnd, &rc) ;
SetWindowPos(hWnd, 0,
(GetSystemMetrics(SM_CXSCREEN) - rc.right) / 2,
(GetSystemMetrics(SM_CYSCREEN) - rc.bottom) / 2,
0, 0, SWP_NOZORDER | SWP_NOSIZE);
resetStats();
ShowWindow(hWnd, nCmdShow);
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}
void doUpdate(Stats *stats, int value)
{
stats->count++;
stats->total += value;
if (value < stats->minimum) {
stats->minimum = value;
}
if (value > stats->maximum) {
stats->maximum = value;
}
}
void update(Stats *stats, int value)
{
if (value != 0) {
doUpdate(stats, abs(value));
}
}
void format(char *buffer, Stats *stats) {
sprintf(buffer, "% 2d <% 5.2f <% 3d",
stats->count == 0 ? 0 : stats->minimum,
stats->count == 0 ? 0.0 : ((double)stats->total / stats->count),
stats->maximum);
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg) {
case WM_LBUTTONDOWN:
resetStats();
printf("Reset.\n");
break;
case WM_MOUSEMOVE: {
int x = GET_X_LPARAM(lParam);
int y = GET_Y_LPARAM(lParam);
if (!motionDetected) {
motionDetected = TRUE;
TRACKMOUSEEVENT tme = { sizeof(tme) };
tme.dwFlags = TME_LEAVE;
tme.hwndTrack = hWnd;
TrackMouseEvent(&tme);
} else {
int dx = x - lastX;
int dy = y - lastY;
update(&hStats, dx);
update(&vStats, dy);
format(buffer1, &hStats);
format(buffer2, &vStats);
printf("OS\t%+d, %+d\t%s\t%s\n", dx, dy, buffer1, buffer2);
}
lastX = x;
lastY = y;
break;
}
case WM_MOUSELEAVE:
motionDetected = FALSE;
break;
case WM_MOUSEWHEEL: {
int delta = GET_WHEEL_DELTA_WPARAM(wParam);
printf(" OS WHEEL %+d (%+.2f)\n", delta, (double)delta / WHEEL_DELTA);
break;
}
case WM_INPUT: {
if (!motionDetected) break;
UINT dwSize = 40;
static BYTE lpb[40];
GetRawInputData((HRAWINPUT)lParam, RID_INPUT,
lpb, &dwSize, sizeof(RAWINPUTHEADER));
RAWINPUT* input = (RAWINPUT*)lpb;
if (input->header.dwType == RIM_TYPEMOUSE) {
int dx = input->data.mouse.lLastX;
int dy = input->data.mouse.lLastY;
if (dx != 0 || dy != 0) {
update(&hRawStats, dx);
update(&vRawStats, dy);
format(buffer1, &hRawStats);
format(buffer2, &vRawStats);
printf("RAW\t%+d, %+d\t%s\t%s\n", dx, dy, buffer1, buffer2);
}
if (input->data.mouse.usButtonFlags & RI_MOUSE_WHEEL) {
short delta = input->data.mouse.usButtonData;
printf("RAW WHEEL %d (%.2f)\n", delta, (double)delta / WHEEL_DELTA);
}
}
break;
}
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
FillRect(hdc, &ps.rcPaint, (HBRUSH) (COLOR_WINDOW + 1));
EndPaint(hWnd, &ps);
break;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}