-4

I'm creating a code-mirror instance with an autocomplete system that will activate on any key press. But whenever you type: { the auto complete still appears even though there is an if statement blocking key code 219 from showing the autocomplete. The reason why I want to stop the { from opening the autocomplete menu is that the user would usually press Enter to go down a line but it puts in the suggestion from the autocomplete. I've already tried using charCode but that didn't work.

here is my code:

editor.on("keyup", function (cm, event) {
            if (!cm.state.completionActive && event.keyCode != 13 && event.keyCode != 219) { 
                CodeMirror.commands.autocomplete(cm, null, {completeSingle: false});
            }
        });

I want it to not open the autocomplete menu in codemirror whenever the user presses {

Username 2020
  • 101
  • 2
  • 10

2 Answers2

0

You are using && with Enter key and { so it will never go into the if condition

editor.on("keyup", function (cm, event) {
        if (!cm.state.completionActive && (event.keyCode != 13 || event.charCode != 219)) { 
            CodeMirror.commands.autocomplete(cm, null, {completeSingle: false});
        }
    });
Murali Krishna
  • 619
  • 2
  • 9
  • 16
  • No, you are in the condition you are saying `Enter` && `{` should be pressed, it should be an `||` because you can't press `Enter and {` at the same time, it's two different events – Murali Krishna Mar 30 '19 at 18:52
0

I figured it out by replacing keyCode with charCode and changing the event to keypress.

editor.on("keypress", function (cm, event) {
            if (!cm.state.completionActive && event.charCode != 13 && event.charCode != 123) {
                CodeMirror.commands.autocomplete(cm, null, {completeSingle: false});
            }
        });
Username 2020
  • 101
  • 2
  • 10