RecordUndo when space, enter, backspace key pressed and some refactoring.