system mouse events

How do I capture system mouse events in Delphi4.
I only want to execute a process after a right mouse click anywhere on the screen.
gertpienaarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

intheCommented:
well if its going to be systemn wide you need to make a "hook dll" like so:

library Hookdemo;

uses

  Beeper in '\DELDEMOS\HOOKDEMO\BEEPER.PAS';


exports
       SetHook index 1,
       UnHookHook index 2,
       HookProc index 3;

begin
   HookedAlready:=False;
end.


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

, where beeper.pas is like so:


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

unit Beeper;

interface

uses Wintypes,Winprocs,Messages;

function SetHook:Boolean;export;
function UnHookHook:Boolean;export;
function HookProc(Code:integer; wParam: Word; lParam: Longint): Longint;export;

var HookedAlready:Boolean;

implementation

var
   ourHook:HHook;


function SetHook:Boolean;
begin
if HookedAlready then exit;
ourHook:=SetWindowsHookEx(WH_MOUSE,HookProc,HInstance,0);
HookedAlready:=True;
end;

function UnHookHook:Boolean;
begin
UnHookWindowsHookEx(ourHook);
HookedAlready:=False;
end;

function HookProc(Code:integer; wParam: Word; lParam: Longint): Longint;
begin
   if (wParam=WM_LBUTTONDOWN) then MessageBeep(0);
   result:=CallNextHookEx(ourHook,Code,wParam,lParam);
end;

end.


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

Now if you call the SetHook function from an application there's a beep everytime you
 press the left mouse button - this continues until you call the UnHookHook function.
 In an actual application you're supposed to call CallNextHookEx immediately and
do nothing else if code < 0 .


you can get the cursor posistion by  using the following on a mousedown event or in the hookproc of the dll

var
p : tpoint;
begin
getcursorpos(p.x,p.y); {now x,y hold mouse co-ordinates}

Regards Barry

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
intheCommented:
credits:
cheers madshi ;-)
0
gertpienaarAuthor Commented:
Thanks a lot

Gert Pienaar
0
gertpienaarAuthor Commented:
Barry.

I received an compiler error on the following line of code:
ourHook:=SetWindowsHookEx(WH_MOUSE,HookProc,HInstance,0);
I changed this to:
ourHook:=SetWindowsHookEx(WH_MOUSE,@HookProc,HInstance,0);
and it compiled OK.

I added the following code to an application:

function SetHook:Boolean; stdcall; external Hookdemo.dll';
function UnHookHook:Boolean; stdcall; external 'Hookdemo.dll';
function HookProc(Code:integer; wParam: Word; lParam: Longint):Longint; stdcall; external 'Hookdemo.dll';

and

procedure TForm1.Button2Click(Sender: TObject);
begin
  SetHook;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  UnhookHook;
end;

Nothing happened when running the program and clicking the mouse

I then loaded this application as a Host Application into the Hookdemo project to try and debug. When clicking on button2 the code in the Sethook function is executed, but when clicking the left mouse, the code in the HookProc is not execvuted and nothing happens.
Clicking Button3 executes the UnhookHook code.

Any suggestions why it doesn't execute the code in the HookProc.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.