Autor Beitrag
phombaa
Hält's aus hier
Beiträge: 10



BeitragVerfasst: Mo 05.05.03 18:18 
hello,

Ich möchte gerne zählen, wieviele Links/Rechts/Mittel-Tasten Klicks ein Benutzer macht. (egal in welchem Programm) Wie könnte ich das realisieren/ Ansatz?

(Es sollte keine Dll verwendet werden)

phombaa.
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 05.05.03 18:46 
SystemHook. Ups soll ja keine DLL verwendet werden. Dann sehe ich keine Lösung.
Und warum keine DLL? :roll:
toms
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1099
Erhaltene Danke: 2



BeitragVerfasst: Mo 05.05.03 18:50 
Hi,

Habe mal was ähnliches gecoded. Darum kann ich dir etwas Code liefern.
Hab's mit einem WH_JOURNALRECORD Hook gelöst, der laut msdn nicht in einer Dll sein muss.
Ist leider unkommentiert aber sollte nicht so schwierig sein.

(ungetestet!)

ausblenden volle Höhe Quelltext
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.