25-04-2011

jQuery Detect cursor key events

One of the things that jQuery makes so much easier in javascript is detecting keyboard events crossbrowser. Here's a simple example detecting the cursor / arrow keys up down left and right:

$(document).keydown(function(e){
 
	switch(e.which){
		case 40:	
			alert('down');
			break; 
		case 39:
			alert('right');
			break;
		case 38:
			alert('up');
			break;
		case 37:
			alert('left')
			break; 
	}
});
 
Keywords: keyboard

Comments:

Your comment:

»

 

[x]