I'm trying to write in c program which analyzes statistically the using of some keyboard keys. First I want to create a keylogger using global hook and log it to file.
Here is the first part of the code i wrote:
#include <stdio.h>
#include <Windows.h>
HHOOK hook;
LRESULT CALLBACK hook_proc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (wParam == WM_KEYDOWN)
{
KBDLLHOOKSTRUCT kbdstruct = *((KBDLLHOOKSTRUCT*)lParam);
char ch = kbdstruct.vkCode;
printf("%c", ch);
}
return CallNextHookEx(hook, nCode, wParam, lParam);
}
void main()
{
MSG msg;
hook = SetWindowsHookEx(WH_KEYBOARD_LL, HOOKPROC(hook_proc), NULL, 0);
while (GetMessage(&msg, NULL, 0, 0))
{
}
}
- Why do I have to create Infinite loop? it doesnt work without it.
- I read that for global hook the second parameter in
SetWindowHookEx
should point to the hook procedure in EXTERNAL DLL. It works fine that why only printing the virtual code. How do I convert it to "regular keys" without switch case for every virtual key? Is there a effective way? - If an external dll is required how should it be written and called from the main based on the code I wrote?