I recently made some modifications to a client’s website that involved using key presses to trigger certain events. I had the numeric keycode for a few of the keys that were already in use, but I didn’t really have a good way of identifying the keyCode for the additional key events I wanted to make use of.

Googling for the result left me with a lot of links that had incorrect information. I searched a little more and found a small Javascript snippit that was supposed to tell you what the keycode was when you pressed down a key, but I found it to be incomplete.

I took that snippit and expanded it to work in IE and Firefox. I’ve included the source here, but you can also check out the working demo.

Here is the source for my little keycode function. Keep in mind that I am using jQuery to update the input box with the correct keycode value.

1
2
3
4
5
6
7
8
<script type="text/javascript" src="jquery-1.2.3.js"></script>
<script type="text/javascript">
function keyCheck(evt){
	key = (evt.which) ? evt.which : evt.keyCode;
	$("#keyCode").val(key);
	//alert("KeyCode = "+key);
}
</script>