I am using the following code to get mouse messages on the current process.
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
}
For some reason when this code runs the mouse get slow for several seconds and then back to normal.
Any ideas?
Thanks
EDIT - hook method
private static IntPtr mouseEvent(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
{
MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
LastLeftClick = new ClickInfo { Time = DateTime.Now, X = hookStruct.pt.x, Y = hookStruct.pt.y };
}
return CallNextHookEx(hookID, nCode, wParam, lParam);
}
public class ClickInfo
{
public int X { get; set; }
public int Y { get; set; }
public DateTime Time { get; set; }
}
WH_MOUSE_LL
requires Windows to forward all mouse events to your application, wait for them to be processed, and then continue as normal. Mouse messages - especially if you have a high-resolution mouse - come fast and furious: it doesn't take long for them to pile up if they're not quickly dispatched. And the first time your callback is called, it likely won't be responding quickly: your process must first perform its own processing and then the runtime must JIT your callback and the P/Invoke call to CallNextHookEx(). – ZeroApplication.Run()
(to start the message-loop) and my mouse lagged because of it too. Dunno why exactly that is, but I'm too lazy to figure it out now that it's not a problem anymore. :D – Beadle