Best Cross-Browser Method to Capture Ctrl+S with Jquery

Best cross-browser method to capture CTRL+S with JQuery?

$(window).keypress(function(event) {
if (!(event.which == 115 && event.ctrlKey) && !(event.which == 19)) return true;
alert("Ctrl-S pressed");
event.preventDefault();
return false;
});

Key codes can differ between browsers, so you may need to check for more than just 115.

how to capture CTRL+S in browser

Not a big difference from the jQuery implementation:

window.addEventListener("keypress", function(event) {
if (!(event.which == 115 && event.ctrlKey) && !(event.which == 19)) return true
alert("Ctrl-S pressed")
event.preventDefault()
return false
})

Capture CTRL+S cross-browser with ExtJS 4.x and avoid browser action

Here is how I usually do it in ExtJS apps (last case statement below), it seems to work well for me:

// attach key navigation to document
Ext.getDoc().on('keypress', function(event, target) {
if (event.ctrlKey && !event.shiftKey) {
event.stopEvent();

switch(event.getKey()) {

case event.LEFT :
this.shiftTabs(-1);
break;

case event.RIGHT :
this.shiftTabs(1);
break;

case event.DELETE :
this.closeActiveTab();
break;

case event.F4 : // this is actually the "S" key
this.saveAll(); // handler
break;

// other cases...
}
}
});

Handling 'ctrl+s' keypress event for browser

This is to just add a different implementation to the question used by me.
Adapted from a SO answer.Also,works for MAC

 document.addEventListener("keydown", function(e) {
if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
e.preventDefault();
//your implementation or function calls
}
}, false);

jQuery capture CTRL + S and Command + S (Mac)

This code works for me. I tested it in Chrome (v33) Firefox (v24) and Safari (v6.1.1 ). Both Control+S and Command+S work.

$(document).keydown(function(event) {
// If Control or Command key is pressed and the S key is pressed
// run save function. 83 is the key code for S.
if((event.ctrlKey || event.metaKey) && event.which == 83) {
// Save Function
event.preventDefault();
return false;
};
}
);

Please note that I am using keydown and not keypress. In the jQuery docs they sate:

Note: as the keypress event isn't covered by any official
specification, the actual behavior encountered when using it may
differ across browsers, browser versions, and platforms.

I would avoid using it.

jquery keypress event for cmd+s AND ctrl+s

Use the event.metaKey to detect the Command key

$(document).keypress(function(event) {
if (event.which == 115 && (event.ctrlKey||event.metaKey)|| (event.which == 19)) {
event.preventDefault();
// do stuff
return false;
}
return true;
});

Ctrl+S Triggers Function but only if inside of specific Text Area JQuery

A couple of similar questions:

  • Best cross-browser method to capture CTRL+S with JQuery?

  • CTRL + S to submit form and all inputs

I bet that first one will get you well on your way to coming up with a solution. Just replace $(window) in the sample code in the accept answer with $('#yourtextareaid').



Related Topics



Leave a reply



Submit