admin管理员组

文章数量:1325761

I used the extraKeys-option of CodeMirror 3.12 to detect when the user starts a new line:

extraKeys: {
    "Enter": onNewLine
}

onNewLine() does nothing but a console.log(). Now CodeMirror ignores that key. You can't start a new line anymore. Is there a way to hook up additional functionality on a new-line-event without interfering CodeMirror internals? I just want to analyze the text of the recently closed line.

I used the extraKeys-option of CodeMirror 3.12 to detect when the user starts a new line:

extraKeys: {
    "Enter": onNewLine
}

onNewLine() does nothing but a console.log(). Now CodeMirror ignores that key. You can't start a new line anymore. Is there a way to hook up additional functionality on a new-line-event without interfering CodeMirror internals? I just want to analyze the text of the recently closed line.

Share Improve this question asked Apr 21, 2013 at 8:34 user414873user414873 7838 silver badges20 bronze badges 1
  • Did you try other key events to make sure it is just this event that is not working? Checkout this post, maybe this helps you stackoverflow./questions/5902683/… – YvesR Commented Apr 21, 2013 at 11:20
Add a ment  | 

2 Answers 2

Reset to default 5

Add a line break at the end of onNewLine function. This should work

 function onNewLine(e){
    console.log(e);
    editor.replaceSelection("\n" ,"end");
  }

I found that returning CodeMirror.Pass also works:

function onNewLine(e) {
    console.log("Enter key was pressed!");
    return CodeMirror.Pass;
}

From the documentation:

A key handler function may return CodeMirror.Pass to indicate that it has decided not to handle the key, and other handlers (or the default behavior) should be given a turn.

This seems to work even if the handler does perform an action. In my case I was using the editor.indentLine function to indent the current line when the user pressed the enter key.

本文标签: javascriptCodeMirror Catching Enter Key prevents line breaksStack Overflow