I am using a global mouse event handler that was originally posted here.
The code posted in the original question works perfectly except that for some reason when I am using it, it is only triggered exactly once. (After that, it never gets triggered again.)
// Subscribe to Event, (placed this in constructor)
MouseHook.Start();
MouseHook.MouseAction += new EventHandler(Event);
// ...
// This function only gets triggered once
private void Event(object sender, EventArgs e)
{
// Do something
}
But, through some experimentation, I did get it working so that it does get triggered with each mouse click now (not just the first one).
I changed the function from this:
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
{
MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
MouseAction(null,new EventArgs());
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
To this:
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
{
MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
MouseAction(null,new EventArgs());
// Added these two lines, works perfectly now
stop();
Start();
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}
But I have no idea why 1) I needed to add these extra lines for my code, and 2) no one else seems to be having this problem.
Can someone explain:
- Why am I experiencing this issue in the first place, and
- Is my "workaround" shown above valid?