moved code that handles the previous keypress