You may have heard of PawSense, a Windows-only utility that prevents keystrokes from being entered when it believes there is a cat or other animal on the keyboard typing nonsense input like "zlxkkkkkkkk;". It seems like a fun project to do in my spare time but I was wondering about some details of implementing it.

I think I could do the pattern recognition part of it, either with hard-coded heuristics or using some kind of pattern recognition algorithm (which I've been exposed to before and feel comfortable with). My question is about the systems programming side of things and the logic for how you'd block input.

You'd need to have your application observe input and present a challenge if you detect cat typing. Would you keep a buffer of recent keystrokes and only let them through if it was non-cat typing? Or if there was cat typing occurring, would you only let a small number through before blocking input? What would be the actual logic you'd want to use?

As for the actual mechanics of blocking input in, say, C#, there are other questions about on this site. My question, to reiterate, is what logic you'd use to detect and block cat typing.

You need to look into introducing a WIN32 systemwide hook. This MSDN article covers the basics of this (attention it specifies both application or thread level hooks and system-wide hooks).

Once you understand that, you'll need to catch several keyboard related messages, WM_KEYUP for sure, a few other WM_KEYxxxx messages as well, depending on your needs).

You'll then need to introduce the logic which decides whether keystrokes are valid or gibberish, and either pass-on the messages (so they can eventually trickle down to the application which has focus) or ignore them.

Without knowing anything about this, I would start out basing it on how long keys are pressed and not on what keys are actually pressed, thinking that an animal is not going to simply press one key and release it very quickly, then another, etc. but is more likely to pause on a set of keys.