Man Gooogler da på Borrisholts kommentar:
http://www.google.com/search?sourceid=navclient&hl=da&ie=UTF-8&oe=UTF-8&q=mouse+hook+delphiEks:
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20355261.htmlhello 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);