Knowing what key has been pressed
2006-10-25 02:54:49
To get to know what key has been pressed in a web page is fairly easy. Just assign a variable to the onkeypressed event and read it, being careful to respect the diference between JScript of internet explorer and the real Javascript from other browsers. That's the meaning of the if conditional lines.
<script language="JavaScript"><!--
document.onkeypress = keyhandler;
function keyhandler(e) {
if (document.layers)
Key = e.which;
else
Key = window.event.keyCode;
if (Key != 0)
alert("Key pressed! ASCII-value: " + Key);
}
//-->
</script>