Is there a way to make Ctrl + Enter behave exactly the same way as Enter in div with contenteditable true

258 Views Asked by At

I have tried many answers from SO for this, but none are perfect. Is there a good solution for this?

Edit 1: I have tried this code:

if (event.keyCode === 13 && event.ctrlKey) {
    //console.log("enterKeyDown+ctrl");
    // $(editor).append('<div><br></div>');
    event.preventDefault();
    if (window.getSelection) {
        let selection = window.getSelection(),
            range = selection.getRangeAt(0),
            br = document.createElement("br");
        range.deleteContents();
        range.insertNode(br);
        range.setStartAfter(br);
        range.setEndAfter(br);
        range.collapse(false);
        selection.removeAllRanges();
        selection.addRange(range);
        return false;
    }

}

But the problem is, when I press ctrl + enter at the end of line, the cursor doesn't go down although inspecting it shows that there's a <br> below it. Plessing Ctrl + Enter in the mid of text works fine.

0

There are 0 best solutions below