Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- jQuery.fn.shortcuts = function(keyset) {
- var element = this;
- element.pressed = new Array();
- element.compare = function(a, b) {
- if (a.length != b.length) return false;
- for (var i = 0; i < a.length; i++) {
- if (a[i] != b[i]) return false;
- }
- return true;
- }
- jQuery(document).keydown(function(event) {
- element.pressed.push(event.keyCode);
- element.pressed.sort();
- for(combo in keyset) {
- if (element.compare(keyset[combo].keys.sort(), element.pressed)) {
- keyset[combo].func();
- }
- };
- });
- jQuery(document).keyup(function(event) {
- element.pressed.splice(jQuery.inArray(event.keyCode, element.pressed), 1);
- });
- return this;
- };
- ## Usage
- $(document).shortcuts({
- "Q" : {
- keys: [81],
- desc: "Log Q to the console",
- func: function() { console.log("Pressed Q") }
- },
- "ALT+I" :{
- keys: [18, 73],
- desc: "Log ALT+I to the console",
- func: function() { console.log("Pressed ALT + I") }
- }
- })
Add Comment
Please, Sign In to add comment