Is it possible to handle line breaks in PHP using keyboard input detection instead of built-in functions like nl2br()?

When handling line breaks in PHP using keyboard input detection, you can listen for the "Enter" key press event and manually insert the line break character "\n" into the text where the cursor is located. This allows you to control the line breaks in the text input without relying on built-in functions like nl2br().

<textarea id="text-input" onkeydown="handleLineBreak(event)"></textarea>

<script>
function handleLineBreak(event) {
    if (event.key === 'Enter') {
        event.preventDefault();
        let cursorPosition = document.getElementById('text-input').selectionStart;
        let text = document.getElementById('text-input').value;
        let newText = text.substring(0, cursorPosition) + '\n' + text.substring(cursorPosition);
        document.getElementById('text-input').value = newText;
    }
}
</script>