Each time it has happened, it's when the CPU has been very busy. I thought their might be some bug where the driver was somehow receiving input from a specific program that was running. One was a 3D game, Cube 2: Sauerbraten. However, the others have just been while compiling large programs. I think the only thing in common is the high CPU usage.
I ended up hoping I could at least workaround the problem by disabling the hardware associated with it. First I tried disabling the touchpad on my computer (an HP Compaq tc4400), and that did not help. Next I tried disabling the stylus (it's a tablet PC) and that worked. In particular, I used
$ xinput list # find my stylus
$ xinput disable <ID number of my stylus> # mine was 12
One problem might be that my stylus is a third-party one that has been disintegrating since the day I unpacked it.
This thread helped me understand what was probably going on, and has good advice for other HP tablets and machines.