Capturing all text entry including keystroke timing

I’m not sure why I’m having so much trouble with this, but I’m just trying to capture the text and keystroke information in a free text entry field. For example, if a user typed “I am”, the following would be captured:

Text: "I am"
Keystroke, Keypress, KeyUp
SHIFT, 1, 3
i, 2, 3
SPACE, 4, 5
a, 6, 7
m, 8, 9

Is this straightforward to write? (Eventually I would like to display an image while they complete each trial.)

Thanks,
Adam