Listen to input event instead of keypress.

This way the last character typed is included in the saved state.