Is there a way to record a keypress or keystroke with AppleScript?

I was wondering if it is possible to have an Applescript record keystrokes.
For instance if I run the script it will sit and listen for keystrokes and record them to a .txt file or something.

I am doing this for a security class, any help would be Awesome and much appreciated.