Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

disable mouse buttons

Posted on 1998-12-11
11
Medium Priority
?
161 Views
Last Modified: 2010-04-04
I would like to write an app that would run on a win9x machine and be capable of system wide left or right (each one individually) mouse button clicks.

I know from some research that i need to use hooks thus requiring me to use a dll. right? Ive never made a dll or used hooks. please help.  some source would be a great help.
thanx
0
Comment
Question by:joesmith69
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 8

Expert Comment

by:ZifNab
ID: 1350701
Hi joesmith69,

Here is an example, hope it still works. Try it out

Building Mouse Hooks

From: David Ullrich <ullrich@math.okstate.edu>


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

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 .

Regards, Zif.
0
 

Author Comment

by:joesmith69
ID: 1350702
Zif,
When compiling it dies on:
ourHook:=SetWindowsHookEx(WH_MOUSE,HookProc,HInstance,0);
with the error:
incompatible types: calling conventions differ.
all the help says is:
The compiler has detected a difference between the declaration and use of a procedure.

huh? sorry the solution may be obvious but not to me. any ideas what the problem could be. if i remark out that line the thing compiles fine but thats where all the good stuff i want to do happens.
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1350703
I think if you change this from

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

to

function HookProc(Code:integer; wParam: Word; lParam: Longint): Longint;export; stdcall;

all will be well.

Cheers,

Raymond.

0
Independent Software Vendors: 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!

 

Author Comment

by:joesmith69
ID: 1350704
sorry to be so much trouble guys...
i changed the function as Raymond described & the compiler barfs @the same line still:                   right here
                                           |
                                           v
ourHook:=SetWindowsHookEx(WH_MOUSE,HookProc,HInstance,0);

and says incompatiable types: integer and word. i changed the following line from:
function HookProc(Code:integer;wParam:Word;lParam:Longint)
                  :Longint;export; stdcall;
to
function HookProc(Code:integer;wParam:integer;lParam:Longint)
                  :Longint;export; stdcall;

which got it to compile but it doesnt work when i call the sethook function. what am i missing??





0
 

Author Comment

by:joesmith69
ID: 1350705
that arrow was supposed to point between "hookproc" and "Hinstance" but it didnt quite work out the way i planned. sorry.

0
 
LVL 20

Accepted Solution

by:
Madshi earned 100 total points
ID: 1350706
Hi Joe,

Zif's example was written for Delphi1/16bit windows. That's why there were so many problems. Here comes the 32bit version:

library mouseDll;

uses Windows,Messages;

var ourHook : HHook;

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

procedure SetHook;
begin
  if ourHook<>0 then exit;
  ourHook:=SetWindowsHookEx(WH_MOUSE,HookProc,GetModuleHandle('mouseDll'),0);
end;

procedure UnHookHook;
begin
  if ourHook=0 then exit;
  UnHookWindowsHookEx(ourHook);
  ourHook:=0;
end;

exports SetHook    index 1,
        UnhookHook index 2;

begin
  ourHook:=0;
end.

Regards, Madshi.
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1350707
Took the words right out of my mouth (it was obvious what the problem was after Joes clarfication!) I admit the export did look strange in my suggestion but didn't act on the hunch - oh well.

Cheers Madshi!

Raymond.
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1350708
Thank you, Raymond...   :-)
0
 
LVL 8

Expert Comment

by:ZifNab
ID: 1350709
I see problem is solved already. See you guys!
0
 

Author Comment

by:joesmith69
ID: 1350710
well madshi thanks for the input, but that didnt work either.

it complied fine..... but again didnt do anything when calling the sethook and unhookhook..... but no matter...
i found a great component to do the trick @
ftp://ftp.uniovi.es/pub4/delphi/ftp/d30free/fngb2302.exe
thanks for all your input
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1350711
I'm sorry, Joe. You must have done something wrong. I tested it and it worked great!!!

Regards, Madshi.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

773 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