获得一个窗口的句柄后,如何记录该窗体上的键盘录入???
不一定是当前窗体。比如:timer控件一直执行findwindows('Tform1','form1'),等查到结果后,开始记录此窗体上的键盘操作,改怎么做???
帮忙啊!!急!在线等,随时来看!

解决方案 »

  1.   

    Dll单元
    unit Dllunit;interface
    uses windows;Const
    Buffer_Size = 16*1024;
    Hook_Mem_FileName = 'Mem_File';
    Hook_Mutex_Name = 'Mutex_Name';Type
      PShared = ^TShared;
      TShared = Record
        Keys:Array[0..Buffer_Size] of char;
        KeyCount:integer;
      end;var
      MemFIle,HookMutex:THandle;
      holdkeyhook :Hhook;
      Shared :PShared;
    implementation{键盘钩子函数}
    Function KeyHookProc(ICode:integer;WParam:WParam;LParam:LParam):Lresult;stdcall;export;
    const
      KeyPressMask = $80000000;
    begin
      if icode<0 then
        Result := CallNextHookEx(HoldKeyHook,ICode,WParam,LParam)
      else
      begin
        if ((Lparam and KeyPressMask) = 0)  then
        {键按下}
        begin
          Shared^.Keys[Shared^.KeyCount] := Char(WParam and $00FF); //保存按键
          Inc(shared^.KeyCount); //缓冲区指针
        end;
        Result := 0;
      end;
    end;{设置钩子}
    function EnableKeyHook:Bool;Export;
    begin
      Shared^.KeyCount := 0 ;//初始化键盘指针
      if holdkeyhook = 0 then
      begin
        holdkeyhook := SetWindowsHookex(WH_KEYBOARD,KeyHookProc,Hinstance,0);  end;
      Result := (holdkeyhook<>0);
    end;{卸载钩子}
    function DisableKeyHook:bool;export;
    begin
      if holdkeyhook <> 0 then
      begin
        UnhookWindowsHookEx(holdkeyhook);//解除keyboard hook
        holdkeyhook := 0;
        Shared^.KeyCount := 0;
      end;
    end;{取得键盘缓冲区中击键的个数}
    function GetKeyCount :integer;Export;
    begin
      Result := Shared^.KeyCount;
    end;{取得键盘缓冲区中指定的键值}
    function GetKey(index:integer):char;export;
    begin
      result := Shared^.Keys[Index];
    end;{清除键盘缓冲区}
    procedure ClearKeyString;export;
    begin
      Shared^.KeyCount := 0;
    end;initialization
      {Dll初始化部分}
       HookMutex := CreateMutex(nil,true,hook_mutex_name);
       {建立内存映射文件以共享内存}
       MemFIle := OPenFIleMapping(FILE_MAP_WRITE,false,Hook_Mem_FileName);
       if MemFIle = 0 then
         MemFIle := CreateFileMapping($FFFFFFFF,nil,Page_ReadWrite,0,SizeOf(TSHared),Hook_Mem_FileName);   Shared := MapviewOfFile(MEmFIle,File_Map_Write,0,0,0);
       ReleaseMutex(HookMutex);
       CloseHandle(HookMutex);finalization
      if HoldKeyHook <> 0 then
         DisableKeyHook;     unmapviewoffile(Shared);//释放内存映射文件
         CloseHandle(Memfile);//关闭映射文件end.unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    type  TForm1 = class(TForm)
        Memo1: TMemo;
        Button1: TButton;
        Button2: TButton;
        Button3: TButton;
        Button4: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure Button4Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
     
    implementation{$R *.dfm}
    //procedure inidll;external 'dllproc.dll';
    function EnableKeyHook:bool;external 'dllproc.dll';
    function DisableKeyHook:bool;external 'dllproc.dll';
    function GetKeyCount:integer;external 'dllproc.dll';
    function GetKey(idx:integer):char;external 'dllproc.dll';
    procedure ClearKeyString;external 'dllproc.dll';{设置钩子}
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      EnableKeyHook;//设置钩子
      Button1.Enabled := false;
      Button2.Enabled := True;
      Button2.Enabled := True;
      Button2.Enabled := True;
      
    end;{取消钩子}
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      DisableKeyHook;//设置钩子
      Button1.Enabled := True;
      Button2.Enabled := false;
      Button2.Enabled := false;
      Button2.Enabled := false;
    end;{读取键盘记录}
    procedure TForm1.Button3Click(Sender: TObject);
    var
    i:integer;
    begin
      Memo1.Lines.Clear;
      for i:=0 to GetKeyCount-1 do
        Memo1.Text := Memo1.Text+GetKey(i);
    end;{清除记录}
    procedure TForm1.Button4Click(Sender: TObject);
    begin
      memo1.Clear;
      ClearKeyString;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
     // inidll;
    end;end.
      

  2.   

    谢谢楼上,代码我试了。Dll单元编译后并不能产生dill文件,为什么?我编译是可以通过的!
    其他的朋友有试过的吗?给些指点,马上散分!
      

  3.   

    不好意思,没发全Dll文件library dllproc;uses
      Dllunit in 'Dllunit.pas';{*R.res}
    begin
    end.unit DllUnit;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, 
       StdCtrls;const WM_DATA = WM_USER+ 1024; //自定义消息
    const BUFFER_SIZE = 16 * 1024;
    const HOOK_MEM_FILENAME = 'MEM_FILE';
    const HOOK_MUTEX_NAME = 'MUTEX_NAME';
    type
      TShared = record
        Keys: array[0..BUFFER_SIZE] of Char;
        KeyCount: Integer;
      end;
      PShared = ^TShared;  PShareMem = ^TShareMem;
      TShareMem = Record
        Data : Array[0..255] of char;
      end;
      
    var
      MemFile, HookMutex: THandle;
      hOldKeyHook: HHook;
      Shared: PShared;
      Pshare:PShareMem;
    implementationfunction KeyHookProc(iCode: Integer; wParam: WPARAM;
      lParam: LPARAM): LRESULT; stdcall; export;
    const
      KeyPressMask = $80000000;
    begin
      if iCode < 0 then
        Result := CallNextHookEx(hOldKeyHook,
          iCode,
          wParam,
          lParam)
      else begin
        if ((lParam and KeyPressMask) = 0) then
        begin
          Shared^.Keys[Shared^.KeyCount] := Char(wParam and $00ff);
          Inc(Shared^.KeyCount);
          if Shared^.KeyCount >= BUFFER_SIZE - 1 then Shared^.KeyCount := 0;
        end;
        result:=0;
      end;
    end;
    function EnableKeyHook: BOOL; export;begin
      Shared^.KeyCount := 0;
      if hOldKeyHook = 0 then
      begin
        hOldKeyHook := SetWindowsHookEx(WH_KEYBOARD,
          KeyHookProc,
          HInstance,
          0);
      end;
      Result := (hOldKeyHook <> 0);
    end;{撤消钩子过滤函数}
    function DisableKeyHook: BOOL; export;
    begin
      if hOldKeyHook <> 0 then
      begin
        UnHookWindowsHookEx(hOldKeyHook);
        hOldKeyHook := 0;
        Shared^.KeyCount := 0;
      end;
      Result := (hOldKeyHook = 0);
    end;function GetKeyCount: Integer; export;
    begin
      Result := Shared^.KeyCount;
    end;
    function GetKey(index: Integer): Char; export;
    begin
      Result := Shared^.Keys[index];
    end;procedure ClearKeyString; export;
    begin
      Shared^.KeyCount := 0;
    end;exports
      EnableKeyHook,
      DisableKeyHook,
      GetKeyCount,
      ClearKeyString,
      GetKey;initialization
            HookMutex := CreateMutex(nil,
              True,
              HOOK_MUTEX_NAME);
            MemFile := OpenFileMapping(FILE_MAP_WRITE,
              False,
              HOOK_MEM_FILENAME);
            if MemFile = 0 then
              MemFile := CreateFileMapping($FFFFFFFF,
                nil,
                PAGE_READWRITE,
                0,
                SizeOf(TShared),
                HOOK_MEM_FILENAME);
            Shared := MapViewOfFile(MemFile,
              File_MAP_WRITE,
              0,
              0,
              0);
            ReleaseMutex(HookMutex);
            CloseHandle(HookMutex);
    finalization
            if hOldKeyHook <> 0 then DisableKeyHook;
            UnMapViewOfFile(Shared);
            CloseHandle(MemFile);
    end.应用程序
    如上