7

I'm trying to figure out how to prevent the browser from displaying the save dialog with a ctrl-s or cmd-s event in Codemirror. I can get the extraKeys to work, I just can't get it to avoid calling resuming the event. I've tried return false and I've dug through the documentation and samples. Does anyone know how to prevent the default from happening?

Here's the code:

var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers: true, mode: "text/html",
   extraKeys: {
      "Ctrl-S": function (instance) {
         alert("your mom");
         return false;
      },
      "Cmd-S": function (instance) {
         alert("my mom");
         return false;
      }
   }
}); 
uadrive
  • 1,249
  • 14
  • 23
  • 1
    Your code seems to work just fine if focus is on editor. why not completely disable Ctrl+S http://stackoverflow.com/questions/11000826/ctrls-preventdefault-in-chrome – aljordan82 Mar 23 '13 at 01:09
  • It's a good point. I was hoping to use the editor to manage the save process, but in the end, I will probably have to follow your suggestion. Thank you for your input. – uadrive Mar 23 '13 at 07:31
  • 2
    In Firefox, `alert` is what causes the default save file popup to appear. If you actually need to call alert there, `setTimeout(function(){alert("some mom")}, 1)` works!! – vikki Mar 15 '14 at 10:26

1 Answers1

4

This is an old question and the solution is already inside the answer, but for anyone looking for a solution to implement Ctrl-S / Cmd-S using CodeMirror, I guess there is a better solution as noted in CodeMirrror documentaion

CodeMirror.commands.save = function() {
    /* Do your stuff */
};
Özgür Uysal
  • 371
  • 1
  • 3
  • 13