把它加到你的project 中,调用时run 停止时stop
unit unit2;
interface
uses Windows,Messages, SysUtils,TLHelp32;
const
KeyMask = $80000000;
var
HHExtendKeyProc:HHook;
procedure stop;stdcall;
procedure run;stdcall;
implementation
//消息钩子截获键盘输入
function LogProc(nCode:integer;wParam:WPARAM;lParam:LPARAM):LRESULT;stdcall;
var
x,y:integer;
WndClassName: array[0..254] of char;
CurClass:STring;
begin
if (peventmsg(lparam)^.message = WM_LBUTTONDOWN) then
begin
x:=peventmsg(lparam)^.paramL;
y:=peventmsg(lparam)^.paramH;
GetClassName(peventmsg(lparam)^.hwnd, @WndClassName, 254);
CurClass:= StrPas(WndClassName);
If (UPPerCase(CurClass)<>'TEDIT') then
begin
//判断x,y坐标是否在你画的button 内如在
// sendMessage(....);
end;
end;
Result:=CallNextHookEx(HHExtendKeyProc,nCode,wParam,lParam);
end;
//启动停止钩子
procedure SetHook(fSet:boolean);
begin
if fSet=true then
begin
if HHExtendKeyProc=0 then
begin
HHExtendKeyProc:=SetWindowsHookEx(WH_JOURNALRECORD,@LogProc,hinstance,0);
end;
end
else
begin
if HHExtendKeyProc<>0 then
UnhookWindowsHookEx(HHExtendKeyProc);
HHExtendKeyProc:=0;
end;
end;
// 停止
procedure stop;stdcall;
begin
SetHook(False);
end;
//开始
procedure run;stdcall;
begin
SetHook(true);
end;
end.