Skip to content

Instantly share code, notes, and snippets.

@codeachange
Created December 11, 2013 15:50
Show Gist options
  • Save codeachange/7912811 to your computer and use it in GitHub Desktop.
Save codeachange/7912811 to your computer and use it in GitHub Desktop.
jQuery double keydown plugin, detect double keydown(same key pressed twice) event
(function($){
$.fn.dblkeydown = function(keyCode, callback){
return $(this).keydown(function(e){
if (e.which == keyCode) {
var lastTime = $(this).data('dblkeydown_' + keyCode);
var curTime = new Date().getTime();
if (lastTime && (curTime - lastTime < $.fn.dblkeydown.maxInterval)) {
$(this).data('dblkeydown_' + keyCode, null);
callback.call(this);
}else{
$(this).data('dblkeydown_' + keyCode, curTime);
};
};
});
};
$.fn.dblkeydown.maxInterval = 500;
})(jQuery);
@ambastos
Copy link

ambastos commented Apr 28, 2018

Wow, very easy and useful solution :D. Congrats!
But, I regret a bit. It works, but conflicts with one keypress

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment