Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - How to trigger a function when Ctrl + S is pressed?

   $(window).keypress(function(event) {
    if (event.which == 115 && event.ctrlKey){
        myfunction();
    }
   });
   myfunction(){
    alert("Key pressed Ctrl+s");
   }

When Ctrl+S was pressed, I don't see this myfunction is trigger. Can anyone help.? I am new to jQuery. Thanks in advance.

like image 526
Mark's Enemy Avatar asked Feb 09 '23 17:02

Mark's Enemy


1 Answers

Listen for keyup and keydown. Also, the key code for 's' is 83. This reliably works:

$(document).bind("keyup keydown", function(e){
    if(e.ctrlKey && e.which == 83){
        myfunction();
    }
});

function myfunction(){
    alert("Key pressed Ctrl+s");
}
like image 188
Andy Noelker Avatar answered Feb 11 '23 15:02

Andy Noelker