Freigeben über


Global hooks getting lost on Windows 7

Hi all,

 

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.

Regards,

 

 

Alex (Alejandro Campos Magencio)

Comments

  • Anonymous
    January 18, 2011
    thank a lot
  • Anonymous
    January 30, 2011
    cheers m8
  • Anonymous
    January 31, 2011
    It was just what i'm looking for! Thanks!
  • Anonymous
    December 12, 2011
    The comment has been removed
  • Anonymous
    July 14, 2013
    Using RawInput method, Hooking works fine but I cannot able to block the Key events. My requirement is to block key like "VK_ENTER". In WinXP, i have used LowLevelKeyboard proc and if i want to block some key event i will return "-1" to block that key before processing by other proc in the application.Please give me some information to block a key in Win7.
  • Anonymous
    April 08, 2014
    Generally, I agree, but beware that an application can only register one window at a time to receive WM_INPUT meessages. If you are writing a control that needs access to mouse or keyboard input and you call RegisterRawInput from the control, this will de-regester any window that the application has already registered and you will definitely not be popular.John