[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 146
  • Last Modified:

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.
0
gertpienaar
Asked:
gertpienaar
  • 2
  • 2
1 Solution
 
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
 
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now