We Are All Monica
We Are All Monica

Reputation: 13336

Did MS change something about keyboard hooks in Windows Vista or 7?

I've implemented keyboard hooks in several languages (AutoIt, C#) using SetWindowsHookEx and WH_KEYBOARD_LL. I also know of a couple of C++ programs that have the same issue.

I didn't post any code because they work perfectly in Windows XP. However, under Windows 7, at some point the hooks become "unloaded" or stop processing any further keys. It seems like it may be related to a low memory condition, but I'm not really sure.

Did Microsoft change the way keyboard hooks work in Vista or 7 to add some logic that would unload third-party hooks under certain circumstances?


Related questions:

how to restart a mouse hook?
Detecting Keyboard Hooks

Upvotes: 1

Views: 1158

Answers (2)

Nick Spreitzer
Nick Spreitzer

Reputation: 10588

I ran into the same timeout issue you describe when I was writing my own keyboard hook the other day. To get around the issue, I wrote my hookcallback proc such that it calls a key press event asynchronously and immediately returns.

Here's a link to my code, if you're interested.

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 941317

Well, it's been tinkered with plenty when UAC was implemented in Vista. Nevertheless, this is not a common complaint. Yes, it is quite possible for Windows to stop calling back the hook callback. A built-in feature to prevent the operating system from getting unresponsive when there is one hooker that doesn't handle the callback in a timely manner. It gets automatically removed from the callback list without any diagnostic.

This is based on a timeout and can indeed trip when the OS is starting to run low on resources. Like not having enough RAM and running lots of processes, getting massive paging. More likely with later versions of Windows since they need more RAM and tend to suffer when the machine was upgraded instead of wiped before the install due to disk fragmentation problems (especially the paging file).

The timeout setting can be tweaked by adding the HKCU\Control Panel\Desktop\LowLevelHooksTimeout value (DWORD, say 10000). Ask more questions about it at superuser.com

Upvotes: 3

Related Questions