Skip to content

Instantly share code, notes, and snippets.

@HarishChaudhari
Created February 6, 2015 11:23
Show Gist options
  • Save HarishChaudhari/fd5e30a4b2267eb9de49 to your computer and use it in GitHub Desktop.
Save HarishChaudhari/fd5e30a4b2267eb9de49 to your computer and use it in GitHub Desktop.
CTRL + key detection in JavaScript
/* This may not work with default browser shortcuts. Need to give it a try though. */
/* Create function */
$.ctrl = function(key, callback, args) {
$(document).keydown(function(e) {
if(!args) args=[]; // IE barks when args is null
if(e.keyCode == key.charCodeAt(0) && e.ctrlKey) {
callback.apply(this, args);
return false;
}
});
};
/* Use function */
$.ctrl('S', function() {
alert("Saved");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment