Solved

Global Mouse Double Button Click Detection

Posted on 2002-05-22
22
356 Views
Last Modified: 2010-04-04
I am looking for a System Hook that will allow me to detect when both the left and right mouse buttons have been clicked. I will then need to be able to use that event to fire procedures after detection. Does anyone have something like this...?

Thanks

Q2
0
Comment
Question by:quantum2
  • 12
  • 9
22 Comments
 
LVL 12

Expert Comment

by:Lee_Nover
Comment Utility
if you already have a system mouse hook then you simply check the pressed buttons
from your hook dll send the info on the buttons to your app with PostMessage
then in the app handle the message like:


type
  TBtnSet = set of TMouseButton;

var BtnSet: TBtnSet;

procedure TForm1.WMMyMessage(var Msg: TMessage);
var bothdown: boolean;
begin
  // lParam is the down/up flag, wParam is the button
  case Msg.lParam of
    0: Exclude(BtnSet, TMouseButton(Msg.wParam));
    1: Include(BtnSet, TMouseButton(Msg.wParam));
  end;
  bothdown:=(mbLeft in BtnSet) and (mbRight in BtnSet);
end;

the hook procedure looking smth like :

function hpWSH(nCode: integer; wParam: WPARAM; lParam: LPARAM): LRESULT;stdcall;
begin
     try
        // maybe do some other stuff
        PostMessage(MainWindow, WM_lnHOOK, wParam, lParam);
     finally
        Result:=CallNextHookEx(HookID, nCode, wParam, lParam);
     end;
end;
0
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
I have a tip how to hook all next mouse events .I don't remember the link and is to long to put here . If interested can send it through mail .

 Message.wParam of
     WM_LBUTTONDBLCLK    
     WM_LBUTTONDOWN      
     WM_LBUTTONUP        
     WM_MBUTTONDBLCLK    
     WM_MBUTTONDOWN      
     WM_MBUTTONUP        
     WM_MOUSEMOVE        
     WM_NCHITTEST        
     WM_NCLBUTTONDBLCLK  
     WM_NCLBUTTONDOWN    
     WM_NCLBUTTONUP      
     WM_NCMBUTTONDBLCLK  
     WM_NCMBUTTONDOWN    
     WM_NCMBUTTONUP      
     WM_NCMOUSEMOVE      
     WM_NCRBUTTONDBLCLK  
     WM_NCRBUTTONDOWN    
     WM_NCRBUTTONUP      
     WM_RBUTTONDBLCLK    
     WM_RBUTTONDOWN      
     WM_RBUTTONUP        
0
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
0
 
LVL 1

Author Comment

by:quantum2
Comment Utility
I am guessing that this link is a DLL that does this type of hook. I wish i could read the comments. I am also guessing that this App shows how to use the DLL. I will check it out in more detail tonight.

Q2
0
 
LVL 9

Accepted Solution

by:
ginsonic earned 100 total points
Comment Utility
Yes. Exist an english version on same site , too . Found it later :).

http://www.q3.nu/trucomania/fting.html and go for tip
414 16/03/00 Hooks: Mouse hook at system level  by Radikal (Q3 Team)

You have the dll and demo application. I put the dll part into my application and using without use dll .
0
 
LVL 1

Author Comment

by:quantum2
Comment Utility
Hey thanks...
I rally appreciate it. Good site.

Q2
0
 
LVL 1

Author Comment

by:quantum2
Comment Utility
Well...
I cant get it to work in Win2000. This hook needs to function in both Win95,98,me,NT,2000 and Yes you gussed it... XP.

Does anyone have any ideas. If someone can provide me with working code (DLL + source or a link to it) and a sample of handling the message for the Left+right mouse button click I will give out 300 pts.

I am in a pinch and dont have time to fiddle with this, so a no brainer solution would be apprecaited.

Q2
0
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
I know that. I use it in old Win98 and got problem just in XP .

For this reason,in present I use another project for hooking the mouse. But tested just under XP .
0
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
Forgot . Give me your address to send you the project.
0
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
The dll segment . Mark where to work for your wishes .

library HookDLL;

uses WinTypes,WinProcs,Messages;

var
  HookCount: integer;
  HookHandle: HHook;

{$IFDEF WIN32}
function MouseHookCallBack(Code: integer; Msg: WPARAM; MouseHook: LPARAM): LRESULT; stdcall;
{$ELSE}
function MouseHookCallBack(Code: integer; Msg: word; MouseHook: longint): longint; export;
{$ENDIF}
begin
  if Code >= 0 then begin

    { HERE YOUR JOB <--------------------------------}

    if Msg = WM_LBUTTONDOWN then
        begin
 {******************Your Job ******************}
        end;

{<-----------------------------------------}

  Result := CallNextHookEx(HookHandle, Code, Msg, MouseHook);
  end else
    Result := CallNextHookEx(HookHandle, Code, Msg, MouseHook);
end;

function InstallHook(SystemHook: boolean; TaskHandle: THandle) : boolean; export;
 function GetModuleHandleFromInstance: THandle;
  var
    s: array[0..512] of char;
  begin
    GetModuleFileName(hInstance, s, sizeof(s)-1);
    Result := GetModuleHandle(s);
  end;
begin
  Result := TRUE;
  if HookCount = 0 then begin
    if SystemHook then
      HookHandle := SetWindowsHookEx(WH_MOUSE, MouseHookCallBack, HInstance, 0)
    else
      HookHandle := SetWindowsHookEx(WH_MOUSE, MouseHookCallBack,
                                     GetModuleHandleFromInstance, TaskHandle);
    if HookHandle <> 0 then
      inc(HookCount)
    else
      Result := FALSE;
  end else
    inc(HookCount);
end;

function RemoveHook: boolean; export;
begin
 Result := FALSE;
  if HookCount < 1 then exit;
  Result := TRUE;
  dec(HookCount);
  if HookCount = 0 then
    Result := UnhookWindowsHookEx(HookHandle);
end;

function IsHookSet: boolean; export;
begin
  Result := (HookCount > 0) and (HookHandle <> 0);
end;

exports
  InstallHook,
  RemoveHook,
  IsHookSet,
  MouseHookCallBack;

begin
  HookCount := 0;
  HookHandle := 0;
end.
0
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
Demo application . Button1 install the hook, Button2 ... uninstall .

unit Main;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,Forms, Dialogs, StdCtrls;

type
  THookForm = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  public
  end;

var
  HookForm: THookForm;

const
  HOOK_DLL = 'HOOKDLL.DLL';

implementation

{$R *.DFM}

function InstallHook(SystemHook: boolean; TaskHandle: THandle): boolean; external HOOK_DLL;
function RemoveHook: boolean;  external HOOK_DLL;
function IsHookSet: boolean;   external HOOK_DLL;

function InstallSystemHook: boolean;
begin
  InstallHook(TRUE, 0);
end;

procedure THookForm.FormDestroy(Sender: TObject);
begin
  while IsHookSet do
    RemoveHook;
end;

procedure THookForm.Button1Click(Sender: TObject);
begin
  if not InstallSystemHook then
 ShowMessage('SetWindowsHookEx() failed.');
 button1.enabled := false;
 button2.enabled := true;
end;

procedure THookForm.Button2Click(Sender: TObject);
begin
  if IsHookSet then
    RemoveHook;
     button1.enabled := true;
 button2.enabled := false;
end;

procedure THookForm.FormCreate(Sender: TObject);
begin
 button1.enabled := true;
 button2.enabled := false;
end;

end.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
I see now that wish to hook the right button, too. So


  if Msg = WM_LBUTTONDOWN then
       begin
{******************Your Job ******************}
       end;


  if Msg = WM_RBUTTONDOWN then
       begin
{******************Your Job ******************}
       end;


How I say already. Tested in XP, but don't see any reason to fail in the others Win versions .

Hope that this code to solve your problem ... and mine :)
The promise points will keep me in KPro zone for next two months.
0
 
LVL 1

Author Comment

by:quantum2
Comment Utility
I will have to try this out in a few. I am looking for the hook to catch each mouse button being pressed at the same time simultaneously.

I will see if this does the trick.

Q2
0
 
LVL 1

Author Comment

by:quantum2
Comment Utility
If this one doesnt get both mouse button clicks at once and you want to email the projects, send them to:

iiamsolutions@hotmail.com
0
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
interesting ... I don't get notifications for your comments !? I visit now this topic just to let a comment . Hmm , I will contact the Community Support  to inform about this posible bug ( no comment notification after accepted answer ).

I send you the project file . All what you have to do is to put your condition ( use Message.wParam from my comment 05/22/2002 11:33PM ) in MouseHookCallBack.
0
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
quantum2, you can download it:
http://www.ginsonic.ro/MouseHook.zip
0
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
Now the hook capture the moment when right and left mouse buttons are pressed in same time .
0
 
LVL 9

Expert Comment

by:ginsonic
Comment Utility
Comments ?
0
 
LVL 1

Author Comment

by:quantum2
Comment Utility
Ginsonic,
Hey thanks for the files. I need to add a simple message pass to let me know when the DLL detects the click. My event code to process once this double click is triggered would be in the application that sets the hook.

This is looking good though.

Q2
0
 
LVL 1

Author Comment

by:quantum2
Comment Utility
Ginsonic,
Hey thanks for the files. I need to add a simple message pass to let me know when the DLL detects the click. My event code to process once this double click is triggered would be in the application that sets the hook.

This is looking good though.

Q2
0
 
LVL 1

Author Comment

by:quantum2
Comment Utility
Ginsonic,
Hey thanks for the files. I need to add a simple message pass to let me know when the DLL detects the click. My event code to process once this double click is triggered would be in the application that sets the hook.

This is looking good though.

Q2
0
 
LVL 1

Author Comment

by:quantum2
Comment Utility
ginsonic,
I have sent you a couple of emails. The basic hook works well. I have asked a couple of more questions. Either way, I am going to post at least one new question that is an award for points. Just post an answer to it and I will give you the points.

Q2
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now