Avatar billede cr2000 Nybegynder
08. maj 2003 - 15:10 Der er 9 kommentarer og
1 løsning

"Fang" museklik uden for form?

Overskrift siger det hele.

Hvordan kan jeg køre en procedure når musen klikkes(Også uden for form.)

Har tænk mig at lave en fil hvor alle musekliks gemmes og efter et par dage kan jeg så se hvor på skærmen jeg har klikket mest :)
Avatar billede borrisholt Novice
09. maj 2003 - 09:12 #1
Du skal lave et globalt mouse hook
Avatar billede cr2000 Nybegynder
09. maj 2003 - 14:20 #2
Og det går man hvordan?
Avatar billede cr2000 Nybegynder
09. maj 2003 - 14:21 #3
Gør... (skulle der stå)
Avatar billede lanstorp Nybegynder
09. maj 2003 - 14:27 #4
Man Gooogler da på Borrisholts kommentar:

http://www.google.com/search?sourceid=navclient&hl=da&ie=UTF-8&oe=UTF-8&q=mouse+hook+delphi

Eks: http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20355261.html

hello DelphiRider, the easiest way to get all the systems mouse button Downs and Ups is with the Journal hook. And you don't have to run it from a .DLL library. This code uses a Form with 2 buttons on it, one button starts the journal hook and the other button stops it. There is One List box which will show all the mouse ups and downs and their screen coordinates.
There is also one TApplicationEvents to get the OnMessage event, so you can restart the hook if there's a WM_CANCELJOURNAL message.


unit MouseButton;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, AppEvnts;

type
TForm1 = class(TForm)
  Button_StartJour: TButton;
  Button_StopJour: TButton;
  ListBox1: TListBox;
  ApplicationEvents1: TApplicationEvents;
  procedure Button_StartJourClick(Sender: TObject);
  procedure Button_StopJourClick(Sender: TObject);
  procedure ApplicationEvents1Message(var Msg: tagMSG;
    var Handled: Boolean);
  procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
  { Private declarations }
public
  { Public declarations }
end;

var
Form1: TForm1;
JHook: THandle;
Track: Boolean;

implementation

{$R *.DFM}

function JournalProc(Code, wParam: Integer; var EventStrut: TEVENTMSG): Integer; stdcall;
var
Char1: PChar;
begin
{this is the JournalRecordProc}
Result := CallNextHookEx(JHook, Code, wParam, Longint(@EventStrut));
{the CallNextHookEX is not really needed for journal hook since it it not
really in a hook chain, but it's standard for a Hook}
if Code < 0 then Exit;

{you should cancel operation if you get HC_SYSMODALON}
if Code = HC_SYSMODALON then Exit;
if Code = HC_ACTION then
begin
{the EventStrut record has the Information about the mouse or keyboard
event. You said you just wanted the mouse button events so I get the
mouse down and mouse up event messages}
  if EventStrut.message = WM_LBUTTONUP then
  Form1.ListBox1.Items.Add('Left Mouse UP at X pos '+IntToStr(EventStrut.paramL)
                            +' and Y pos '+IntToStr(EventStrut.paramH));
  if EventStrut.message = WM_LBUTTONDOWN then
  Form1.ListBox1.Items.Add('Left Mouse Down at X pos '+IntToStr(EventStrut.paramL)
                            +' and Y pos '+IntToStr(EventStrut.paramH));
  if EventStrut.message = WM_RBUTTONDOWN then
  Form1.ListBox1.Items.Add('Right Mouse Down at X pos '+IntToStr(EventStrut.paramL)
                            +' and Y pos '+IntToStr(EventStrut.paramH));
  if (EventStrut.message = WM_RBUTTONUP) then
  Form1.ListBox1.Items.Add('Right Mouse Up at X pos '+IntToStr(EventStrut.paramL)
                            +' and Y pos '+IntToStr(EventStrut.paramH));
end;
end;

procedure TForm1.Button_StartJourClick(Sender: TObject);
begin
if Track then
begin
ShowMessage('Mouse is already being Journaled, can not restart');
Exit;
end;

JHook := SetWindowsHookEx(WH_JOURNALRECORD , @JournalProc, 0, 0);
{SetWindowsHookEx starts the Hook}
if JHook > 0 then
begin
Track := True;
end else
ShowMessage('No Journal Hook availible');
end;

procedure TForm1.Button_StopJourClick(Sender: TObject);
begin
Track := False;
UnhookWindowsHookEx(JHook);
JHook := 0;
end;

procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG;
var Handled: Boolean);
begin
{the journal hook is automaticly camceled if the Task manager
(Ctrl-Alt-Del) or the Ctrl-Esc keys are pressed, you restart it
when the WM_CANCELJOURNAL is sent to the parent window, Application}
Handled := False;
if (Msg.message = WM_CANCELJOURNAL) and Track then
JHook := SetWindowsHookEx(WH_JOURNALRECORD , @JournalProc, 0, 0);
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
{make sure you UN hook it if the app closes}
UnhookWindowsHookEx(JHook);
end;

end.



- - - - - - - - - - - - - - - - -  - - - - - - - - - - - -

Hope this is easier and you can use it, ask questions if you need more. 
Comment from DelphiRider  09/11/2002 11:48AM PST 
hellp Slick812, Thank you for your answer. i think it will work, but there is a problem, when i click on start, it shows: No journal hook available. what is the matter?   
Accepted Answer from Slick812  09/11/2002 05:10PM PST 
sorry about that, I copied and pasted code that did somethig else

in the
procedure TForm1.Button_StartJourClick(Sender: TObject);

change the line
JHook := SetWindowsHookEx(WH_JOURNALRECORD , @JournalProc, 0, 0);


to


JHook := SetWindowsHookEx(WH_JOURNALRECORD , @JournalProc, hInstance, 0);
Avatar billede cr2000 Nybegynder
10. maj 2003 - 01:32 #5
Er der så også en som har en ide til hvordan jeg printer en pixel prik ud for hvert kordinat jeg har gemt i en tekst.

Hvis i forstår :)
Avatar billede dkn Nybegynder
13. maj 2003 - 09:42 #6
det kommer da an på hvad du mener med printer?
vil du painte på skrivebordet?
Avatar billede lanstorp Nybegynder
13. maj 2003 - 12:03 #7
Avatar billede cr2000 Nybegynder
14. maj 2003 - 07:52 #8
Har fundet en løsning...


Ikke nogle der vil have pointsne?
Avatar billede borrisholt Novice
18. maj 2003 - 13:40 #9
Jow da .. Men hvad med at dele dem ?
Avatar billede cr2000 Nybegynder
18. maj 2003 - 17:23 #10
Nu får du dem. Fordi der er ikke nogle at dele med...
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester