Some time ago a customer of mine reported the following issue with SetWindowsHookEx API:
Their application had global hooks to monitor for both keyboard and mouse input. On Windows 7, and under high CPU usage, those hooks were getting lost. They tried several things to solve the issue, without success: adjust registry setting “Control Panel\Desktop\LowLevelKeyboardProc” to 10000 (10sec), restart the hooks every X minutes (with UnhookWindowsHookEx and SetWindowsHookEx), etc. At the end, they had to restart the app to reactivate the hooks.
After some research I found out the following:
On Windows 7 we have to make sure that the callback function of the hook can return in less than LowLevelHooksTimeout, which is 300 ms. And we allow for the application to be timed out 10 times when processing the hook callback message. If it times out an 11th time, Windows will unhook the application from the hook chain. This is a by design feature and it was added in Win7 RTM.
My recommendation is that low level hooks should be avoided whenever possible. If you are monitoring keystrokes (and not trying to block them), you can get the keyboard input via Raw Input. This is lighter weight than hooks, won’t affect other apps’ responsiveness, and won’t be turned off if the app isn’t responsive.
My customer got rid of the hooks and used Raw Input instead with success. I hope this helps you too.
Alex (Alejandro Campos Magencio)