{~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~} { Downloaded from The Coder's Knowledge Base } { http://www.netalive.org/ckb/ } {~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~} { @ CKB Header Version.: 1.01 } { @ Category ID........: delphi_misc } { @ Added to database..: 10.11.98 } {~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~} { @ Title..............: Mouse hooks } { @ Original Filename..: mousehook.txt } { @ Author.............: David Ullrich (ullrich@math.okstate.edu) } { @ Description........: Building mouse hooks } { @ Tested w. Compiler.: not tested yet } { @ Submitted by.......: Unofficial Delphi Developers FAQ (uddf@gnomehome.demon.nl) } {~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~} library Hookdemo; uses Beeper in '\DELDEMOS\HOOKDEMO\BEEPER.PAS'; exports SetHook index 1, UnHookHook index 2, HookProc index 3; begin HookedAlready:=False; end. , where beeper.pas is like so: unit Beeper; interface uses Wintypes,Winprocs,Messages; function SetHook:Boolean;export; function UnHookHook:Boolean;export; function HookProc(Code:integer; wParam: Word; lParam: Longint): Longint;export; var HookedAlready:Boolean; implementation var ourHook:HHook; function SetHook:Boolean; begin if HookedAlready then exit; ourHook:=SetWindowsHookEx(WH_MOUSE,HookProc,HInstance,0); HookedAlready:=True; end; function UnHookHook:Boolean; begin UnHookWindowsHookEx(ourHook); HookedAlready:=False; end; function HookProc(Code:integer; wParam: Word; lParam: Longint): Longint; begin if (wParam=WM_LBUTTONDOWN) then MessageBeep(0); result:=CallNextHookEx(ourHook,Code,wParam,lParam); end; end. Now if you call the SetHook function from an application there's a beep everytime you press the left mouse button - this continues until you call the UnHookHook function. In an actual application you're supposed to call CallNextHookEx immediately and do nothing else if code < 0 .