Solved

Global Mouse Double Button Click Detection

Posted on 2002-05-22
22
371 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 9
22 Comments
 
LVL 12

Expert Comment

by:Lee_Nover
ID: 7028549
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
ID: 7029008
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
ID: 7029020
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:quantum2
ID: 7030638
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
ID: 7031251
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
ID: 7032048
Hey thanks...
I rally appreciate it. Good site.

Q2
0
 
LVL 1

Author Comment

by:quantum2
ID: 7032413
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
ID: 7032658
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
ID: 7032674
Forgot . Give me your address to send you the project.
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 7032776
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
ID: 7032821
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
 
LVL 9

Expert Comment

by:ginsonic
ID: 7032868
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
ID: 7033866
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
ID: 7033898
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
ID: 7036437
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
ID: 7036454
quantum2, you can download it:
http://www.ginsonic.ro/MouseHook.zip
0
 
LVL 9

Expert Comment

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

Expert Comment

by:ginsonic
ID: 7037290
Comments ?
0
 
LVL 1

Author Comment

by:quantum2
ID: 7039692
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
ID: 7039776
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
ID: 7039884
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
ID: 7040111
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

628 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