Fix and improve input handling in text editor