unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Mask, RzEdit, RzSpnEdt;
type
TForm1 = class(TForm)
Edit1: TEdit;
RzSpinEdit1: TRzSpinEdit;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
end;
{声明键盘钩子回调函数; 其参数传递方式要用 API 的 stdcall}
function KeyHook(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
Form1: TForm1;
implementation
{$R *.DFM}
var
hook: HHOOK; {定义一个钩子句柄}
{实现键盘钩子回调函数}
function KeyHook(nCode: Integer; wParam: wParam; lParam: lParam): LRESULT;
begin
if (wParam = 65) then
Form1.Edit1.Text:=IntToStr(strtoint(Form1.Edit1.Text)+1);
Result := CallNextHookEx(hook, nCode, wParam, lParam);
end;
{设置键盘钩子}
procedure TForm1.FormCreate(Sender: TObject);
begin
hook := SetWindowsHookEx(WH_KEYBOARD, @KeyHook, 0, GetCurrentThreadID);
end;
{释放键盘钩子}
procedure TForm1.FormDestroy(Sender: TObject);
begin
UnhookWindowsHookEx(hook);
end;
end.