Tip: Capturing keys with javascript

The following code captures all the typing and displays key code on the web page. Click here to view a demo of this code

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Capturing Keys with JavaScript</title>
<script language="javascript"> 
	current_row=1;
	nn=(document.layers)?true:false;
	ie=(document.all)?true:false;
	function keyDown(e) {
		var evt=(e)?e:(window.event)?window.event:null;
		if(evt){
			var key=(evt.charCode)?evt.charCode: ((evt.keyCode)?evt.keyCode:((evt.which)?evt.which:0));
			document.getElementById('key').innerHTML='<h1>Keycode is '+key+'</h1>';
		}
	}
document.onkeydown=keyDown;
if(nn) document.captureEvents(Event.KEYDOWN);
 </script>
</head>

<body>
	<h1>Press a key to get key code...</h1>
    <div id="key"></div>
</body>
</html>

 

More Javascript tips