JavaScript
Article

jQuery Detect if a User Smashes Their Keyboard

By Sam Deering

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();
});
Sam Deering
Meet the author
Sam Deering is a Front-end Web Developer who specialises in JavaScript & jQuery. Sam is driven and passionate about sharing his knowledge to educate others.

No Reader comments

Recommended

Learn Coding Online
Learn Web Development

Start learning web development and design for free with SitePoint Premium!

Get the latest in JavaScript, once a week, for free.