Description

This is used to give the keyCode of the keys that are pressed.

Demonstration

Press a key...

You pressed this key:

It's KeyCode is:

Code

<script type="text/javascript">
function whichKey(e) {
	if (window.event) {
		code = event.keyCode
	}
	else {
		code = e.which;
	}
	if (document.getElementById) {
		document.getElementById("charcode").innerHTML = String.fromCharCode(code);
		document.getElementById("keycode").innerHTML = code;		
	}
	else {
		alert ("You pressed this key:"+String.fromCharCode(code)+"\n\nIt's KeyCode is:"+code);
	}
}
document.onkeypress = whichKey;
document.onkeydown = whichKey;
</script>
</head>
<body>
<p>You pressed this key:
<span id="charcode" style="font-family: verdana, arial, sans-serif; font-weight: bold; font-size: 12px;"></span></p>
<p>It's KeyCode is:
<span id="keycode" style="font-family: verdana, arial, sans-serif; font-weight: bold; font-size: 12px;"></span></p>

Comments:

Tested in: IE6, Opera 7, NN4.7, NN7, and Mozilla 1.2.
Works in: IE6, Opera 7, NN4.7, NN7, and Mozilla 1.2.
Doesn't work in: N/A
Comments: Works best in IE, and works for most keys in other browsers.

Copyright© 2003, Infinity Web Design