jQuery Detect if a User Smashes Their Keyboard

Sam Deering
Tweet

jquery-keyboard-smashed
jQuery plugin to detect if a user has smashed their keyboard. This is determined by the amount of keys that are depressed which triggers the event… nice!

Could be used to calm down those crazy people in the office!

Demo

(function($){
 $.fn.extend({
 
 	onKeyboardSmashed : function(callbackFunction,options) {
		var defaults = {
			//change here for event trigger
			threshold: 3
		};
		
	  var options = $.extend(defaults, options);
	  
	  
	  return this.each(function() {

			var keys_pressed = 0 ;
			$(this).keydown(function(event) {
				keys_pressed++
			
					if( keys_pressed >options.threshold){
						    if(typeof callbackFunction == 'function'){
							  callbackFunction.call(this);
							}
					}
			});
			
			$(this).keyup(function(event) {
				keys_pressed--;
			});
		
	  });
	  
	}
 });
})(jQuery);
//usage
$(document).onKeyboardSmashed(function(){
	$("#status").html('keyboard smashed');
	$("#msg").hide();
});

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

No Reader comments