Wednesday, April 8, 2009

Press any key to determine the javascript key code of that key

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<TITLE>Javascript keyCode checker tool</TITLE>
<script language="JavaScript">
document.onkeydown = checkKeycode
function checkKeycode(e) {
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
//alert("keycode: " + keycode);
}
</script>

</HEAD>
<BODY>
<a onclick="window.event.keyCode = 17+107">test</a>
Press any key to determine the javascript key code of that key.
This is a simple script:
<table bgcolor="#FFFFCC" width="100%" cellspacing="0" cellpadding="5" border="1">
<tr><td>
<pre>
<script language="JavaScript">
document.onkeydown = checkKeycode
function checkKeycode(e) {
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
alert("keycode: " + keycode);
}
</script>
</pre>
</td>
</tr>
</table>
You may also use this sort of function to
disable certain keys, by adding a void(0) in the function as shown.
This essentially tells the page to cancel the last event,
e.g. the pressing of that certain key. In the example below,
the key that is disabled is the enter key.
<table bgcolor="#FFFFCC" width="100%" cellspacing="0" cellpadding="5" border="1">
<tr><td>
<pre>
<script language="JavaScript">
document.onkeydown = checkKeycode
function checkKeycode(e) {
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
if(keycode == 13){
void(0);
}
}
</script>

</pre>
</td>
</tr>
</table>

</BODY>
</HTML>

No comments: