protected void handleKeyTyped(char keyChar) {
        if (keyChar == 8) { // backspace
            if (cursorIndex > 0) {
                moveCursor(-1);
                enteredText.deleteCharAt(cursorIndex);
                refreshEndOfString();
            }
        }
        else if (keyChar == 27) // escape
            clearEnteredText();
        else if (keyChar == '\n') { // newline
            moveCursor(enteredText.length() - cursorIndex);
            window.output('\n', textAttributes);
            synchronized (editLock) {
                editLock.notify();
                exitEditMode();
            }
        }
        else if (keyChar > 31) { // non-control character
            enteredText.insert(cursorIndex++, keyChar);
            
            // this moves the cursor, so no moveCursor(1) needed
            if (!passwordMode)
                window.outputImmediately(keyChar, textAttributes); 
            else
                window.outputImmediately('*', textAttributes);
                
            if (cursorIndex < enteredText.length())
                refreshEndOfString();
        }

        //assert cursorIndex >= 0 && cursorIndex <= enteredText.length() : "cursorIndex out of bounds";
    }