0

I see this is very popular question, but i still cannot find right solution.

I'm trying to call function (save) on Ctrl+s pressed inside my textarea. I found that Chrome or IE needs "keydown" and FireFox needs "keypress".

Actually, Firefox registers "keydown" - calls my "alert" but don't prevent browser "Save As" dialog.

I tried to put both events, - then FireFox prevents Save As dialog, but calls my Alert twice.

Here's code:

$("#mytextarea").on("keypress keydown",function(e) { 
    if ((e.which == '115' || e.which == '83' ) && (e.ctrlKey || e.metaKey)) {
         e.preventDefault();
         alert("Ctrl-s pressed");
         return false;
    }
    return true;
});

JS fiddle - try to open with FF

Does anyone have real crossbrowser solution?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Yuray
  • 747
  • 1
  • 9
  • 19

2 Answers2

0

For a save browser compatibility you can also use one of the the following javascript libraries:

with jquery for example:

muetzerich
  • 5,600
  • 7
  • 37
  • 52
  • Still no "safe" xbrowser solution... check what happens with mice: https://jsfiddle.net/jy4kfxeL/ - Firefox will fire alert and open dialog window – Yuray Jun 13 '16 at 16:13
0

I found hint that looks like working solution at: Can't override ctrl+s in Firefox using jQuery Hotkeys

it looks like i should wrap function in setTimeout... it works for now.

Community
  • 1
  • 1
Yuray
  • 747
  • 1
  • 9
  • 19