1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105:
| unit Unit1;
interface
uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
TMouseClicks = record Left: Integer; Middle: Integer; Right: Integer; end; type TForm1 = class(TForm) label1: TLabel; procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure FormCreate(Sender: TObject); private { Private declarations } FMouseClicksCounter: TMouseClicks; FHookStarted: Boolean; procedure StartJournalHook; procedure StopJournalHook; procedure DisplayMouseClicksCounter; function MessageHook(var Msg: TMessage): Boolean; public { Public declarations } end;
var Form1: TForm1;
implementation
{$R *.dfm}
var JHook: HHOOK;
procedure TForm1.DisplayMouseClicksCounter; begin label1.Caption := Format('L: %d; M: %d; R: %d', [FMouseClicksCounter.Left, FMouseClicksCounter.Middle, FMouseClicksCounter.Right]); end;
function JournalProc(Code, wParam: Integer; var EventStrut: TEventMsg): Integer; stdcall; const MouseMsgs: set of Byte = [WM_LBUTTONUP - $200, WM_RBUTTONUP - $200, WM_MBUTTONUP - $200]; begin Result := CallNextHookEx(JHook, Code, wParam, Longint(@EventStrut)); if Code < 0 then Exit; if Code = HC_SYSMODALON then Exit; if Code = HC_ACTION then begin if (EventStrut.message - $200) in MouseMsgs then begin if EventStrut.message = WM_LBUTTONUP then Inc(FMouseClicksCounter.Left); if EventStrut.message = WM_RBUTTONUP then Inc(FMouseClicksCounter.Right); if EventStrut.message = WM_MBUTTONUP then Inc(FMouseClicksCounter.Middle); Form1.DisplayMouseClicksCounter; end; end; end;
procedure TForm1.StartJournalHook; begin JHook := SetWindowsHookEx(WH_JOURNALRECORD, @JournalProc, hInstance, 0); FHookStarted := JHook > 0; end;
function TForm1.MessageHook(var Msg: TMessage): Boolean; begin Result := False; if (Msg.Msg = WM_CANCELJOURNAL) and FHookStarted then begin JHook := SetWindowsHookEx(WH_JOURNALRECORD, @JournalProc, 0, 0); FHookStarted := JHook > 0; end; end;
procedure TForm1.StopJournalHook; begin if FHookStarted then begin UnhookWindowsHookEx(JHook); FHookStarted := False; end; end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin StopJournalHook; end;
procedure TForm1.FormCreate(Sender: TObject); begin FMouseClicksCounter.Left := 0; FMouseClicksCounter.Middle := 0; FMouseClicksCounter.Right := 0; StartJournalHook; end;
end. |