This happens because my cat likes to sit on my keyboard. Is there any way to prevent this from happening? I enjoy my cat's company while working, so would rather not shut him out.
Since this is Lifehacks, I figure it's fine to offer an alternative solution:
Install a cat typing detector on your computer.
This is software that detects when the keyboard is being operated in a non-humanlike manner (presumably, e.g. with clusters of nearby keys being pressed simultaneously), and locks the keyboard when that happens.
(Source)
Some can also be configured to emit a loud noise when they detect a cat on the keyboard, hopefully annoying the cat enough to keep them away even if you're not around to move them off. Or you can disable that feature and just let your cat lounge on your keyboard as much as she likes, secure in the knowledge that it won't harm your computer (except maybe by getting the fans full of cat hair, but that'll happen anyway).
The one I've personally used, and can testify to the effectiveness of, is PawSense, which the screenshot above comes from. Alas, it's Windows-only, and somewhat pricey too, but my parents own a license and have been quite satisfied with it. And it's even won an IgNobel prize! ;-)
Apparently, there's a program called CatNip that does the same for OS X. Alas, I'm not yet aware of equivalent software for Linux. Apparently, in this particular respect, open source is (so far) inferior! ;-)
Ps. See also this related question on Super User.