这是一个使用HOOK的例子程序,可是却编译不过。unit hook1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
{the prototype for the new keyboard hook function}
function KeyboardHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM):Integer; //LResult; stdcall;
private
{ Private declarations }
public
{ Public declarations }
end;{$R *.dfm}var
Form1: TForm1;
WinHook: HHOOK; // a handle to the keyboard hook functionimplementation{$R *.DFM}procedure TForm1.FormCreate(Sender: TObject);
begin
WinHook := SetWindowsHookEx(WH_KEYBOARD, @KeyboardHook, NULL, GetCurrentThreadID);
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
{remove the keyboard hook function from the keyboard hook chain}
UnhookWindowsHookEx(WinHook);
end;function TForm1.KeyboardHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM):Integer;begin
{if we can process the hook information...}
if (nCode>-1) then
{...was the TAB key pressed?}
if (wParam=VK_TAB) then
begin
{if so, output a beep sound}
MessageBeep(0); {indicate that the message was processed}
Result := 1;
end
else
{...was the RETURN key pressed?} if (wParam=VK_RETURN) then
begin
{if so, and if the key is on the up stroke, cause
the focus to move to the next control}
if ((lParam shr 31)=1) then
Form1.Perform(WM_NEXTDLGCTL, 0, 0); {indicate that the message was processed}
Result := 1;
end
else
{otherwise, indicate that the message was not processed.} Result := 0
else
{we must pass the hook information to the next hook in the chain}
Result := CallNextHookEx(WinHook, nCode, wParam, lParam);
end;
end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
{the prototype for the new keyboard hook function}
function KeyboardHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM):Integer; //LResult; stdcall;
private
{ Private declarations }
public
{ Public declarations }
end;{$R *.dfm}var
Form1: TForm1;
WinHook: HHOOK; // a handle to the keyboard hook functionimplementation{$R *.DFM}procedure TForm1.FormCreate(Sender: TObject);
begin
WinHook := SetWindowsHookEx(WH_KEYBOARD, @KeyboardHook, NULL, GetCurrentThreadID);
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
{remove the keyboard hook function from the keyboard hook chain}
UnhookWindowsHookEx(WinHook);
end;function TForm1.KeyboardHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM):Integer;begin
{if we can process the hook information...}
if (nCode>-1) then
{...was the TAB key pressed?}
if (wParam=VK_TAB) then
begin
{if so, output a beep sound}
MessageBeep(0); {indicate that the message was processed}
Result := 1;
end
else
{...was the RETURN key pressed?} if (wParam=VK_RETURN) then
begin
{if so, and if the key is on the up stroke, cause
the focus to move to the next control}
if ((lParam shr 31)=1) then
Form1.Perform(WM_NEXTDLGCTL, 0, 0); {indicate that the message was processed}
Result := 1;
end
else
{otherwise, indicate that the message was not processed.} Result := 0
else
{we must pass the hook information to the next hook in the chain}
Result := CallNextHookEx(WinHook, nCode, wParam, lParam);
end;
end.
定义到Form外边
try...
不知是否能解决
先用一个临时变量取得该返回值,再在函数中引用