Added functionality to track caret position

Added functionality to track caret position when typing (rather than clicking) keys.

Fixed bug that was causing the press of an undefined key to delete the last character.