Created
February 6, 2015 11:23
-
-
Save HarishChaudhari/fd5e30a4b2267eb9de49 to your computer and use it in GitHub Desktop.
CTRL + key detection in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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