Hook and steal mouse clicks?

Just starting out? Need help? Post your questions and find answers here.
User avatar
kenmo
Addict
Addict
Posts: 2047
Joined: Tue Dec 23, 2003 3:54 am

Hook and steal mouse clicks?

Post by kenmo »

Got a Windows event hooking question here... What I'm trying to do is globally intercept middle-mouse button clicks while either control key is down (yes I know this seems like a very specific strange method...). It's simple enough to catch CONTROL+MBUTTON events using, say, getasynckeystate_(). The only issue bugging me though is that I want to block the mouse click event from going to the active window. For example, you're browsing the web and you want to bring up my ctrl+mclick window, but middle clicking in the browser activates the auto-scroll feature... which is not desired... so I want to steal the event.

Is there a way to do this? Thanks for any help.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

You can use a mouse hook, and if you return non-zero from the hook procedure it will not pass on the press event to any windows. This is the only way I know of to prevent the active window from processing the event.
BERESHEIT
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Where should the hook be installed? Just your PB application or globally?
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

I think it will need to be global. I tried something similar (not exactly like this but kinda) back in PB 4.02 and the only way I could get it to work was globally.
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

Then you could do it like this:

Code: Select all

Procedure MouseHook(nCode,wParam,lParam)
	If wParam = #WM_MBUTTONDOWN And GetAsyncKeyState_(#VK_CONTROL) & 32768 	
		ProcedureReturn 1
	EndIf

   ProcedureReturn CallNextHookEx_(0,nCode,wParam,lParam)
EndProcedure

OpenWindow(0,0,0,240,140,"MouseBlock",#WS_CAPTION | #WS_SYSMENU | 1)

StickyWindow(0,1)

hhkLLMouse = SetWindowsHookEx_(#WH_MOUSE_LL,@MouseHook(),GetModuleHandle_(0),0)

While WaitWindowEvent() ! 16 : Wend

UnhookWindowsHookEx_(hhkLLMouse)
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

The hook procedure has to be compiled to a dll in order to work globally. Otherwise it only works on your own window when it's active.
BERESHEIT
User avatar
Fluid Byte
Addict
Addict
Posts: 2336
Joined: Fri Jul 21, 2006 4:41 am
Location: Berlin, Germany

Post by Fluid Byte »

netmaestro wrote:The hook procedure has to be compiled to a dll in order to work globally.
No, it's hasn't. I tested it with Firefox and it work's like a charm.
Last edited by Fluid Byte on Sun Mar 16, 2008 6:01 pm, edited 1 time in total.
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

netmaestro wrote:The hook procedure has to be compiled to a dll in order to work globally. Otherwise it only works on your own window when it's active.
WH_MOUSE_LL = Low-level (Influences the whole Windows System)
WH_MOUSE = High level (Influences only GetMessage or PeekMessage)

See MSDN... http://msdn2.microsoft.com/en-us/library/ms644959.aspx

If you wanted to make an API Hook you should compile it to a DLL for global hooks. But you don't even have to then.
bye,
Daniel
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

From the article you quote:
MSDN wrote:A global hook procedure can be called in the context of any application in the same desktop as the calling thread, so the procedure must be in a separate DLL module.
There is no ambiguity or gray area here. You must compile to a dll or it won't be effective for all running applications. Windows has to have a dll to inject into the other processes.
BERESHEIT
DarkDragon
Addict
Addict
Posts: 2345
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Post by DarkDragon »

netmaestro wrote:From the article you quote:
MSDN wrote:A global hook procedure can be called in the context of any application in the same desktop as the calling thread, so the procedure must be in a separate DLL module.
There is no ambiguity or gray area here. You must compile to a dll or it won't be effective for all running applications. Windows has to have a dll to inject into the other processes.
But you know what low level means?
The LowLevelMouseProc hook procedure is an application-defined or library-defined callback function used with the SetWindowsHookEx function. The system call this function every time a new mouse input event is about to be posted into a thread input queue. The mouse input can come from the local mouse driver or from calls to the mouse_event function. If the input comes from a call to mouse_event, the input was "injected". However, the WH_MOUSE_LL hook is not injected into another process. Instead, the context switches back to the process that installed the hook and it is called in its original context. Then the context switches back to the application that generated the event.
From this article (Which is a subarticle from the article I gave you):
http://msdn2.microsoft.com/en-us/librar ... S.85).aspx

[EDIT]
In the next posting of netmaestro he indirectly says I'm right. :lol: It's ok, netmaestro, we know I'm right and we also know you're too self-confident to tell us the truth. :lol:

Err.. even Microsoft makes faults in their documentation.
Last edited by DarkDragon on Sun Mar 16, 2008 6:20 pm, edited 2 times in total.
bye,
Daniel
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

But you know what low level means?
Yes. When you were in diapers I knew. And I know what "must be in a separate DLL module" means. I see no point in commenting further on this.
BERESHEIT
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

It really depends on the core of the OS...

It works globally without being in a DLL if the windows is < NT. If NT based (XP, 2k, Vista, etc ) then it needs to be in a DLL.
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

WH_KEYBOARD_LL = no DLL required for global use
WH_MOUSE_LL = no DLL required for global use

all other WH_* = DLL required for global use
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

ic, that may be useful.... ;)
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

I have seen some reports that say using either of the 2 _LL hooks can slow your system down in some cases. I have never used either of them so I can't say first hand if that is true or not.
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
Post Reply