Link to home
Start Free TrialLog in
Avatar of ginsonic
ginsonicFlag for Romania

asked on

Hook the mouse

I found some time ago a good tip how to hook the mouse at www.delphi300.com . Work perfect on Win9x, but when I tested under XP fail .
Same author upload a keyboards hook and this work perfect for both systems!?

SO, my question is:
How can I hook the mouse under Win9x and XP , too .
ASKER CERTIFIED SOLUTION
Avatar of inthe
inthe

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DelFreak
DelFreak

Could you send it to me too. Thanks! delfreak@thedoghousemail.com
Avatar of ginsonic

ASKER

Inthe , sorry for delay .
The code is ~ what I need (and work under XP), except the x and y cursor coordonation.
How can I get these ?

Regards,
Nick

P.S. How get my website address ? :P
er .. my name is neo :)

hehe when you read this you probably remember
 (im sure you would've used this before as quite common though mayb not so anyway here it is )

to Get the Cursor Pos use GetCursorPos() by in HookDLL.Dpr change the callback to:

function MouseHookCallBack(Code: integer; Msg: word; MouseHook: longint): longint; export;
{$ENDIF}
var
p : tpoint;
begin
  if Code >= 0 then begin
    if Msg = WM_RBUTTONDOWN then
        begin
        getcursorpos(p);
         MessageBeep(1);
         Showmessage('x = ' +inttostr(p.x)+', y = '+inttostr(p.y));
        end;
  Result := CallNextHookEx(HookHandle, Code, Msg, MouseHook);
  end else
    Result := CallNextHookEx(HookHandle, Code, Msg, MouseHook);
end;

hope thats what your after,also have to add "sysutils" and "dialogs" to uses section
 (for inttostr and showmessage functions)

Regards Barry
Sorry Barry ...neo :)

I know GetCursorPos function . The tip founded by me on net get the coordonates from message . Don't use another function.
I upload the tip on my site for a short time:
www.ginsonic.ro\hook.txt.
Sorry , try this link www.ginsonic.ro\hook.txt .
interesting way to get it via the message itself,didnt think of that one ;-)
Can you help me to combine your tip with mine ( mine - not really mine - don't work for XP ) to get X,Y points?

I don't have work untill now with hooks and in same time I'm bussy to release my new version of TgnDBGrid .

HELP ME and I will accept your comment with A grade .
two probs,
i dont have xp & i dont know which part is failing (thats why i had to send you example to test).

which parts do you need to combine?
cause i thought the example i sent was working under xp ?

do you want to merge the callbacks ,
very untested :

function MouseHookCallBack(Code: integer; Msg: word; MouseHook: longint): longint; export;
{$ENDIF}
var
DatosMouse : PMouseHookStruct;
Intentos : integer;
begin
 if Code = HC_ACTION then begin
   if Msg = WM_RBUTTONDOWN then
       begin
FicheroM:=OpenFileMapping(FILE_MAP_READ,False,'ElReceptor');
if FicheroM<>0 then
begin
Compartido:=MapViewOfFile(FicheroM,FILE_MAP_READ,0,0,0);
DatosMouse:=Pointer(lparam);
Compartido^.Ventana:=DatosMouse^.hwnd;
Compartido^.x:=DatosMouse^.pt.x;
Compartido^.y:=DatosMouse^.pt.y;
PostMessage(Compartido^.Receptor,CM_MANDA_DATOS,wParam,lParam);
UnmapViewOfFile(Compartido);
CloseHandle(FicheroM);
end;  //FicheroM = 0
  end; //Msg not right mouse button
 Result := CallNextHookEx(HookHandle, Code, Msg, MouseHook);
 end else //code not hc_action
   Result := CallNextHookEx(HookHandle, Code, Msg, MouseHook);
end; //callback
Sorry , sorry , sorry.
Was capture by a project.

I put your last comment in initial DLL . But I receive errors !?!


mousePos:=MapViewOfFile(FM,FILE_MAP_READ,0,0,0);

DataMouse:=Pointer(lparam);
[Error] Hookdll.dpr(40): '(' expected but ')' found

MousePos^.Named:=DataMouse^.hwnd;
MousePos^.x:=DataMouse^.pt.x;
MousePos^.y:=DataMouse^.pt.y;

PostMessage(MousePos^.Receptor,CM_MOUSE,wParam,lParam);
[Error] Hookdll.dpr(44): '(' expected but ',' found

UnmapViewOfFile(MousePos);
CloseHandle(FM);
:see mousehook callback struct ,its different so

:=Pointer(lparam);
should be
:=Pointer(mousehook);
and
(Compartido^.Receptor,CM_MANDA_DATOS,wParam,lParam);
  should be
(Compartido^.Receptor,CM_MANDA_DATOS,msg,mousehook);

the spanish names are confusing for me to read <g>


realise if it actually works you would also need to createfilemapping in your main project files to use the hook.

(see demo of the hook/txt you posted on your website ,i aslo have saved this demo from ages ago i just found it on my comp and the example it comes with shows how to use it so i guess you realise about file mapping issues.)
while im here:
this is a (quite long) text that we sometimes post here for learning about hoks /callbacks filemapping etc,maybe of interest to you:

http://groups.google.com/groups?q=The+following+example+demonstrates+creating+a+system+wide+windows&hl=en&selm=36CF41A4.3069%40gte.net&rnum=1
can't maked to work :(