home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 February / Chip_2000-02_cd.bin / zkuste / Delphi / navody / tip2 / 699.txt < prev    next >
Text File  |  1999-11-15  |  2KB  |  81 lines

  1. {~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  2. { Downloaded from The Coder's Knowledge Base                         }
  3. { http://www.netalive.org/ckb/                                       }
  4. {~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  5. { @ CKB Header Version.: 1.01                                        }
  6. { @ Category ID........: delphi_misc                                 }
  7. { @ Added to database..: 10.11.98                                    }
  8. {~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  9. { @ Title..............: Mouse hooks                                 }
  10. { @ Original Filename..: mousehook.txt                               }
  11. { @ Author.............: David Ullrich (ullrich@math.okstate.edu)    }
  12. { @ Description........: Building mouse hooks                        }
  13. { @ Tested w. Compiler.: not tested yet                              }
  14. { @ Submitted by.......: Unofficial Delphi Developers FAQ (uddf@gnomehome.demon.nl) }
  15. {~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~}
  16.  
  17.  
  18. library Hookdemo;
  19.  
  20. uses
  21.   Beeper in '\DELDEMOS\HOOKDEMO\BEEPER.PAS';
  22.  
  23. exports
  24.        SetHook index 1,
  25.        UnHookHook index 2,
  26.        HookProc index 3;
  27.  
  28. begin
  29.    HookedAlready:=False;
  30. end.
  31.  
  32. , where beeper.pas is like so:
  33.  
  34. unit Beeper;
  35.  
  36. interface
  37.  
  38. uses Wintypes,Winprocs,Messages;
  39.  
  40. function SetHook:Boolean;export;
  41. function UnHookHook:Boolean;export;
  42. function HookProc(Code:integer; wParam: Word; lParam: Longint): Longint;export;
  43.  
  44. var HookedAlready:Boolean;
  45.  
  46. implementation
  47.  
  48. var
  49.    ourHook:HHook;
  50.  
  51.  
  52. function SetHook:Boolean;
  53. begin
  54.   if HookedAlready then exit;
  55.   ourHook:=SetWindowsHookEx(WH_MOUSE,HookProc,HInstance,0);
  56.   HookedAlready:=True;
  57. end;
  58.  
  59. function UnHookHook:Boolean;
  60. begin
  61.   UnHookWindowsHookEx(ourHook);
  62.   HookedAlready:=False;
  63. end;
  64.  
  65. function HookProc(Code:integer; wParam: Word; lParam: Longint): Longint;
  66. begin
  67.    if (wParam=WM_LBUTTONDOWN) then MessageBeep(0);
  68.    result:=CallNextHookEx(ourHook,Code,wParam,lParam);
  69. end;
  70.  
  71. end.
  72.  
  73.  
  74.  
  75.  
  76. 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
  77. function. In an actual application you're supposed to call CallNextHookEx immediately and do nothing else if code < 0 .
  78.  
  79.  
  80.  
  81.