SetWindowsHookEx with WH_MOUSE_LL slows down the mouse for a few seconds

I am using the following code to receive mouse messages in 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 works, the mouse slows down for a few seconds, and then returns to normal.

Any ideas? Thanks

EDIT Method - hook

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; }
}
+3
source share
5 answers

What does your hook procedure look like?

If your process has only one user interface thread, use the message filter instead: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.addmessagefilter.aspx

+2
source

( ++, #) , WH_MOUSE_LL WH_MOUSE ( ). WM_LBUTTONUP WM_RBUTTONUP .

, , , WH_MOUSE_LL , ( ..). , - Windows .

+2

- ; , .

, , , #, , , - JIT .

, , . ++, Marshal.PtrToStructure, , , , .

( ++) , . ++ - , PostMessage, HWND, .

+2

, , , , .

SetHook
LongRunningStartupProcess
,

, . Dispatcher.CurrentDispatcher.BeginInvoke(new Action(SetHook));

DispatchSetHook
LongRunningStartupProcess
SetHook ( )

, , , . , , Thread.Sleep .

+2
source

When you get the hook event, turn off the book, then do your job and, if you really need it, turn hook on again.

This will stop the mouse from lagging.

Stay hooked when you really need to.

+1
source

Source: https://habr.com/ru/post/1754223/


All Articles