Guest User

Untitled

a guest
Jul 16th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. jQuery.fn.shortcuts = function(keyset) {
  2. var element = this;
  3. element.pressed = new Array();
  4.  
  5. element.compare = function(a, b) {
  6. if (a.length != b.length) return false;
  7. for (var i = 0; i < a.length; i++) {
  8. if (a[i] != b[i]) return false;
  9. }
  10. return true;
  11. }
  12.  
  13. jQuery(document).keydown(function(event) {
  14. element.pressed.push(event.keyCode);
  15. element.pressed.sort();
  16.  
  17. for(combo in keyset) {
  18. if (element.compare(keyset[combo].keys.sort(), element.pressed)) {
  19. keyset[combo].func();
  20. }
  21. };
  22. });
  23.  
  24. jQuery(document).keyup(function(event) {
  25. element.pressed.splice(jQuery.inArray(event.keyCode, element.pressed), 1);
  26. });
  27.  
  28. return this;
  29. };
  30.  
  31. ## Usage
  32.  
  33. $(document).shortcuts({
  34. "Q" : {
  35. keys: [81],
  36. desc: "Log Q to the console",
  37. func: function() { console.log("Pressed Q") }
  38. },
  39. "ALT+I" :{
  40. keys: [18, 73],
  41. desc: "Log ALT+I to the console",
  42. func: function() { console.log("Pressed ALT + I") }
  43. }
  44. })
Add Comment
Please, Sign In to add comment