Hook and steal mouse clicks?
Hook and steal mouse clicks?
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.
Is there a way to do this? Thanks for any help.
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
- Fluid Byte
- Addict
- Posts: 2336
- Joined: Fri Jul 21, 2006 4:41 am
- Location: Berlin, Germany
Where should the hook be installed? Just your PB application or globally?
Windows 10 Pro, 64-Bit / Whose Hoff is it anyway?
- Fluid Byte
- Addict
- Posts: 2336
- Joined: Fri Jul 21, 2006 4:41 am
- Location: Berlin, Germany
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?
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
- Fluid Byte
- Addict
- Posts: 2336
- Joined: Fri Jul 21, 2006 4:41 am
- Location: Berlin, Germany
No, it's hasn't. I tested it with Firefox and it work's like a charm.netmaestro wrote:The hook procedure has to be compiled to a dll in order to work globally.
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?
-
- Addict
- Posts: 2345
- Joined: Mon Jun 02, 2003 9:16 am
- Location: Germany
- Contact:
WH_MOUSE_LL = Low-level (Influences the whole Windows System)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 = 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
Daniel
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
From the article you quote:
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.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.
BERESHEIT
-
- Addict
- Posts: 2345
- Joined: Mon Jun 02, 2003 9:16 am
- Location: Germany
- Contact:
But you know what low level means?netmaestro wrote:From the article you quote: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.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.
From this article (Which is a subarticle from the article I gave you):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.
http://msdn2.microsoft.com/en-us/librar ... S.85).aspx
[EDIT]
In the next posting of netmaestro he indirectly says I'm right.


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
Daniel
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
- DoubleDutch
- Addict
- Posts: 3220
- Joined: Thu Aug 07, 2003 7:01 pm
- Location: United Kingdom
- Contact:
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.
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
https://reportcomplete.com <- School end of term reports system
- DoubleDutch
- Addict
- Posts: 3220
- Joined: Thu Aug 07, 2003 7:01 pm
- Location: United Kingdom
- Contact:
ic, that may be useful.... 

https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
https://reportcomplete.com <- School end of term reports system