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;