0

I am having a strange problem. I am creating two hooks for a c# application that I am developing, one for Keyboard and one for mouse using the following code:

//keyboard
SetWindowsHookEx(13, keyBoardDelegate, IntPtr.Zero, 0);
//mouse
SetWindowsHookEx(14, mouseDelegate, IntPtr.Zero, 0);

Both mouse and keyboard hook procedure code look identical expect for the first parameter and the delegates.

Everything works fine I get notification when a user uses mouse or keyboard. The problem arises after I activate the small question (help) button on top of my main window using the code

 this.HelpButton = true;
 this.HelpButtonClicked += new System.ComponentModel.CancelEventHandler(this.AutoStandBy_HelpButtonClicked);

Every time that I click on the help button on top of the window my form freezes for a while and then it responds and executes the code in AutoStandBy_HelpButtonClicked function. In order to stop my application for freezing I have to remove the mouse SetWindowsHookEx function but NOT the keyboard's SetWindowsHookEx. I have been battling with this problem for the past few days with no luck. Does anybody have any idea how to solve the problem?

 private IntPtr MouseHookDelegate(Int32 Code, IntPtr wParam, IntPtr lParam)
        {
            /// Call CallNextHookEx so we do not break the hook
            if (Code < 0)
                return CallNextHookEx(mouseHandle, Code, wParam, lParam);

            /// if a mouse change occured then call the mouse event
            if (MouseMoved != null)
                MouseMoved(this, new EventArgs());

            /// Call CallNextHookEx so we do not break the hook
            return CallNextHookEx(mouseHandle, Code, wParam, lParam);
        }

Then in my main form

  mi = new MouseInput();
  ///register the mouse event to check for movement
  mi.MouseMoved += mouse_MouseMoved;
mpc
  • 67
  • 1
  • 9
  • The "freezes for a while" is what causes the hook to be destroyed, Windows makes sure that a borken callback cannot make the machine unusable. You posted the wrong code so low odds that you're close to solving this problem. Something wrong with your "mouseDelegate" target method, we can't see it. You've got 5 seconds to hit Debug + Break All with the keyboard to see where it is stuck. Without the keyboard hook in place of course :) – Hans Passant Oct 10 '14 at 11:38
  • Hello I have updated the code. Maybe this help you a bit. Just a not this code is identical to the one with the keyboard delegate. – mpc Oct 10 '14 at 11:55
  • Use a `Stopwatch` and measure how long the call to `MouseMoved` takes. – Ben Voigt Oct 10 '14 at 12:37
  • I am not in the office so I will try it as soon as get back. What should I expect to find? Do you think is something wrong with my function MouseHookDelegate? – mpc Oct 10 '14 at 13:37
  • The delegate looks fine. It's the event handler (`MouseMoved`) that is taking too long. – Mike Caron Oct 10 '14 at 14:13
  • The mouseMoved contained a call to a system timer, MouseTimer.Start(); After changing to MouseTimer.Enabled = true; the problem seems to have gone away! However the same one I had for keyboard KeyboardTimer.Start(); but the problem does not appear there. Can someone shed some light at to why this is happening? Thanks – mpc Oct 10 '14 at 18:15

0 Answers0