Skip to content

Instantly share code, notes, and snippets.

@pifantastic
Created January 4, 2012 00:19
Show Gist options
  • Save pifantastic/1557738 to your computer and use it in GitHub Desktop.
Save pifantastic/1557738 to your computer and use it in GitHub Desktop.
jQuery keyhold plugin. Records the number of milliseconds a key is pressed.
(function($) {
var k = {};
$(window).bind('keydown', function(e) {
if (!k[e.which]) {
k[e.which] = +new Date;
}
});
$(window).bind('keyup', function(e) {
e.duration = new Date - k[e.which];
e.type = 'keyhold';
delete k[e.which];
$(e.target).trigger(e);
});
})(jQuery);
(function(a){var b={};a(window).bind("keydown",function(a){b[a.which]||(b[a.which]=+(new Date))}),a(window).bind("keyup",function(c){c.duration=new Date-b[c.which],c.type="keyhold",delete b[c.which],a(c.target).trigger(c)})})(jQuery)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment