C# low level mouse hook and form event handling
Asked Answered
U

1

6

I'm using a simple form generated by VS 2010 which contains 2 buttons, start and stop. Start triggers WH_MOUSE_LL using SetWindowsHookEx, and stop stops the hook. The hook works fine and I mange to "replace" middle mouse button click with double click, the only problem I have is clicking on Minimize/Maximize/Close buttons of the form, it seems that there is some sort of "event race" between the hook and events called by buttons mentioned above. It reflects on the fact that when you press one of those buttons it keeps being "pressed" for a while before it completes it's action (e.g. form minimizes). When I right click the form and select one of those actions it responds immediately same thing goes when I stop the hook and press one of the buttons mentioned above. Have anyone encountered such behavior ?

Unsnarl answered 29/8, 2011 at 16:18 Comment(7)
My first instinct is that you're probably using the wrong tool for the job - I can't think of many circumstances when you should need to use a Windows hook. Perhaps if you could go back to the problem that led to you implementing this solution, and posted a question about that instead?Leeannaleeanne
Hi,as far as I know and I did quite a research before approaching this method that if I want my generic mouse to produce double click when I click middle mouse button is to "intercept" middle click and simulate two left clicks to the same x,y of the mouse and if I want it to work even when my app. is not in focus or minimized(or at system tray) hook is the only way. Any way I'm more c/c++ programmer I've just wanted to use C# because the simplicity of creating a GUI.Unsnarl
@Unsnarl - Your research lead you to the wrong conclusion. Just capture the middle mouse button and do the same event as a double click mouse event ( which really is just a single click event ). Besides not all mice have the middle mouse button so be watchtful of that.Beaverbrook
How exactly you capture middle mouse button state globally in C# (even if your app. is not in focus) ? Regarding the double click, it is exactly what I do, send two simulated clicks to mouse x,y. I'm afraid that the cause to my problem is the run.application starts the form and the form starts the hook, so when I click on one of the form's buttons the message pump needs to pump all the messages that the hook received and didn't dispatch before it can send the event to the form (e.g. close form event)Unsnarl
I'd be curious to see your callback method. Too much to post?Foil
Hi all,I've figured it out, all I had to do is to override wndproc and catch messages regarding left clicks on form's controls then I disabled the hook return the message to base letting the form to create it's "own" corresponding events depending on the clicked button e.g. formclosing event. I hope this will help to someone in the future because I've encounter the same problem with hook that was posted on some site that I found while I was trying to solve this issue.Unsnarl
@Alex, maybe you should post your last comment as the answer since that seems to be the case. I guess that means the one that I have provided is wrong...Latton
L
2

I have experienced this before as well. I'm not sure as to the exact cause, but I have always solved the problem by listening for events that are fired when the mouse enters and leaves the client area of the form, window, whatever. You can use those events to hook and unhook the mouse, and then you will get the normal behaviour.

Latton answered 1/9, 2011 at 14:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.