MouseKeyHook.dpr 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. library MouseKeyHook;
  2. { Important note about DLL memory management: ShareMem must be the
  3. first unit in your library's USES clause AND your project's (select
  4. Project-View Source) USES clause if your DLL exports any procedures or
  5. functions that pass strings as parameters or function results. This
  6. applies to all strings passed to and from your DLL--even those that
  7. are nested in records and classes. ShareMem is the interface unit to
  8. the BORLNDMM.DLL shared memory manager, which must be deployed along
  9. with your DLL. To avoid using BORLNDMM.DLL, pass string information
  10. using PChar or ShortString parameters. }
  11. uses
  12. Windows,
  13. Classes,
  14. Messages,
  15. SysUtils;
  16. const HOOK_MEM_FILENAME = 'MEM_FILE';
  17. type
  18. TShared = record
  19. LastInputTick: Cardinal;
  20. end;
  21. PShared = ^TShared;
  22. var
  23. MemFile: THandle;
  24. Shared: PShared;
  25. MouseHk,
  26. KeyHK: HHOOK;
  27. {$R *.res}
  28. //------------------------------------------------------------------------------
  29. function KeyHookProc(nCode: Integer; WParam: WPARAM; LParam: LPARAM): LRESULT; stdcall;
  30. begin
  31. Result := 0;
  32. if nCode < 0 then
  33. Result := CallNextHookEx(KeyHK,
  34. nCode,
  35. wParam,
  36. lParam)
  37. else
  38. begin
  39. Shared^.LastInputTick := GetTickCount;
  40. end;
  41. end;
  42. //------------------------------------------------------------------------------
  43. function MouseHookProc(nCode: Integer; WParam: WPARAM; LParam: LPARAM): LRESULT; stdcall;
  44. begin
  45. Result := 0;
  46. if nCode < 0 then
  47. Result := CallNextHookEx(MouseHk,
  48. nCode,
  49. wParam,
  50. lParam)
  51. else
  52. begin
  53. if (WPARAM = WM_LBUTTONDBLCLK) or
  54. (WPARAM = WM_RBUTTONDBLCLK) then
  55. begin
  56. Shared^.LastInputTick := GetTickCount;
  57. end;
  58. end;
  59. end;
  60. //------------------------------------------------------------------------------
  61. function GetLastInputTick: Cardinal; stdcall; export;
  62. begin
  63. Result := Shared^.LastInputTick;
  64. end;
  65. //------------------------------------------------------------------------------
  66. procedure SetLastInputTick(Value: Cardinal); stdcall; export;
  67. begin
  68. Shared^.LastInputTick := Value;
  69. end;
  70. //------------------------------------------------------------------------------
  71. function EnableMouseHook: Boolean; stdcall; export;
  72. begin
  73. if MouseHk = 0 then MouseHk := SetWindowsHookEx(WH_MOUSE,@MouseHookProc,HInstance,0);
  74. if MouseHk = 0 then
  75. begin
  76. Result := False;
  77. end
  78. else
  79. Result := True;
  80. end;
  81. //------------------------------------------------------------------------------
  82. function DisableMouseHook:Boolean; stdcall; export;
  83. begin
  84. Result := UnHookWindowsHookEx(MouseHk);
  85. end;
  86. //------------------------------------------------------------------------------
  87. function EnableKeyHook: Boolean; stdcall; export;
  88. begin
  89. if KeyHk = 0 then KeyHk := SetWindowsHookEx(WH_KEYBOARD,@KeyHookProc, HInstance, 0);
  90. if KeyHk = 0 then
  91. begin
  92. Result := False;
  93. end
  94. else
  95. Result := True;
  96. end;
  97. //------------------------------------------------------------------------------
  98. function DisableKeyHook:Boolean; stdcall; export;
  99. begin
  100. Result := UnHookWindowsHookEx(KeyHk);
  101. end;
  102. procedure DllEntry(dwReason : integer);
  103. begin
  104. case dwReason Of
  105. // DLLµ¼Èë½ø³Ìʱ
  106. DLL_PROCESS_ATTACH:
  107. begin
  108. MemFile := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, HOOK_MEM_FILENAME);
  109. if MemFile = 0 then
  110. MemFile := CreateFileMapping(DWORD(-1), nil,
  111. PAGE_READWRITE,
  112. 0,
  113. SizeOf(TShared),
  114. HOOK_MEM_FILENAME);
  115. Shared := MapViewOfFile(MemFile,
  116. FILE_MAP_ALL_ACCESS,
  117. 0,
  118. 0,
  119. SizeOf(TShared));
  120. Shared^.LastInputTick := GetTickCount;
  121. end;
  122. DLL_PROCESS_DETACH:
  123. begin
  124. UnmapViewOfFile(Shared);
  125. CloseHandle(memFile);
  126. end;
  127. end;
  128. end;
  129. exports
  130. EnableMouseHook,
  131. DisableMouseHook,
  132. EnableKeyHook,
  133. DisableKeyHook,
  134. GetLastInputTick,
  135. SetLastInputTick;
  136. begin
  137. DllProc := @DllEntry;
  138. DllEntry(DLL_PROCESS_ATTACH);
  139. end.