Recording all text input

I would like to make a AppleScript that runs in the background and captures all text I input while the script is running and saves it to a plain text file. I have no idea on how to go about doing this, or even if it is possible.
If it is possible, I would greatly appreciate a code example.

Intercepting your keyboard events is not possible with AppleScript. Maybe with Cocoa?