Skip to content
Browse files

jQuery Shortcuts

  • Loading branch information...
0 parents commit d0586277e36765a452a8045ed44f3d133a10d484 @ambethia ambethia committed Oct 28, 2009
Showing with 84 additions and 0 deletions.
  1. +25 −0 README.md
  2. +56 −0 jquery.shortcuts.js
  3. +3 −0 jquery.shortcuts.min.js
25 README.md
@@ -0,0 +1,25 @@
+# jQuery Shortcuts
+
+A very lightweight approach to keyboard shortcuts (hotkeys) with [jQuery](http://jquery.com/).
+
+## Example Usage
+
+ $(document).shortcuts({
+ "Q": {
+ keys: [81],
+ desc: "Log Q to the console",
+ func: function() { console.log("You pressed Q") }
+ },
+ "ALT I": {
+ keys: [18, 73],
+ desc: "Log this key combination to the console",
+ func: function() { console.log("You pressed ALT and I") }
+ }
+ });
+
+## License
+
+Copyright © 2009 Jason L Perry
+
+Dual licensed under the MIT and GPL licenses.
+Uses the same license as [jQuery](http://docs.jquery.com/License).
56 jquery.shortcuts.js
@@ -0,0 +1,56 @@
+/*
+ * jQuery Shortcuts
+ *
+ * A very lightweight approach to keyboard shortcuts (hotkeys) with jQuery.
+ *
+ * Example Usage:
+ *
+ * $(document).shortcuts({
+ * "Q": {
+ * keys: [81],
+ * desc: "Log Q to the console",
+ * func: function() { console.log("You pressed Q") }
+ * },
+ * "ALT I": {
+ * keys: [18, 73],
+ * desc: "Log this key combination to the console",
+ * func: function() { console.log("You pressed ALT and I") }
+ * }
+ * });
+ *
+ * Copyright (c) 2009 Jason L Perry
+ * Dual licensed under the MIT and GPL licenses.
+ * Uses the same license as jQuery, see http://docs.jquery.com/License
+ *
+ * @version 0.1
+ */
+
+jQuery.fn.shortcuts = function(keys) {
+ var self = this;
+ this.pressed = new Array();
+
+ jQuery(document).keydown(function(event) {
+ self.pressed.push(event.keyCode);
+ for(combo in keys) {
+ if (!self.focused && self.compare(keys[combo].keys, self.pressed)) {
+ keys[combo].func();
+ };
+ };
+ });
+
+ jQuery(document).keyup(function(event) {
+ self.pressed.splice(jQuery.inArray(event.keyCode, self.pressed), 1);
+ });
+
+ jQuery("input, select, textarea").bind("focus blur", function(event) {
+ self.focused = event.type == "focus";
+ })
+
+ this.compare = function(a, b) {
+ if (a.length != b.length) return false; a.sort(); b.sort();
+ for (var i=0; i < a.length; i++) { if (a[i] != b[i]) return false; };
+ return true;
+ }
+
+ return this;
+};
3 jquery.shortcuts.min.js
@@ -0,0 +1,3 @@
+jQuery.fn.shortcuts=function(keys){var self=this;this.pressed=new Array();jQuery(document).keydown(function(event){self.pressed.push(event.keyCode);for(combo in keys){if(!self.focused&&self.compare(keys[combo].keys,self.pressed)){keys[combo].func();};};});jQuery(document).keyup(function(event){self.pressed.splice(jQuery.inArray(event.keyCode,self.pressed),1);});jQuery("input, select, textarea").bind("focus blur",function(event){self.focused=event.type=="focus";})
+this.compare=function(a,b){if(a.length!=b.length)return false;a.sort();b.sort();for(var i=0;i<a.length;i++){if(a[i]!=b[i])return false;};return true;}
+return this;};

0 comments on commit d058627

Please sign in to comment.
Something went wrong with that request. Please try again.