javascript - Can't catch Cmd-S on Chrome on Mac -


i'm trying catch both ctrl-s , cmd-s on browsers cross-os compatibility of web app. saw thread how here: jquery keypress event cmd+s , ctrl+s

i have following snippet in code:

$(document).keypress(function(event) {   if (event.which == 115 && (event.ctrlkey||event.metakey)|| (event.which == 19)) {     event.preventdefault();     save();     return false;   }   return true; }); 

where save() javascript function send ajax request in future, has alert('saved!'); now.

however, although catches ctrl-s, doesn't catch cmd-s on chrome, instead opening save webpage dialog usual. saw else on page had same problem, didn't see solution it.

thanks in advance!

i think keypress have doesn't register metakeys in quite same way, see: diffrence between keyup keydown keypress , input events here's fiddle seems work using keydown, , capturing each in sequence. hope helps?

var metaflag = false;    $(document).on({  	keydown: function(event) {      if (event.ctrlkey||event.metakey || event.which === 19) {        event.preventdefault();        $('.monitor').text('key '+event.which);        metaflag = true;      }    	if( metaflag && event.which === 83 ){ // 83 = s?        event.preventdefault(); // maybe not necessary        $('.display').text('saving?');        metaflag = false;      }    }  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>    <div class='monitor'></div>  <div class='display'></div>


Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -