简体   繁体   中英

SetWindowsHookEx WH_MOUSE_LL Hook only takes 1 mouse movement

I am setting a global hook with the following code:

SetWindowsHookEx(WH_MOUSE_LL, MouseProc, NULL, 0)

I have a breakpoint set so that when I first run the application I can see that the MouseProc method is called. This works but after the first time it is no longer called. Is the Hook automatically removed, how do I get this so that the hook automatically persists? I am writing this application for windows and this is a C++ win32 project.

I have a breakpoint set

That's enough to explain the problem. The debugger will break of course. Which prevents further mouse messages from being delivered to the window with the focus. Windows puts up with that for 5 seconds, then unceremoniously disables your hook because it thinks your code is broken.

The timeout is configurable, you can use Regedit.exe to set the HKEY_CURRENT_USER\\Control Panel\\Desktop\\LowLevelHooksTimeout value. Not present by default, add the DWORD value first. The unit is milliseconds.

First you need to do:

return CallNextHookEx(_hook, nCode, wParam, lParam);

in your MouseProc , so it should return.

Second, it's not possible to debug most of codes which does have hook, your app will crash, because windows expecting your MouseProc function to return as soon as possible with return. Also your code should have Message handling code, like GetMessage , TranslateMessage and DispatchMessage .

Example code: SetWindowsHookEx for WH_MOUSE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM