|
-
Mar 7th, 2006, 09:35 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] keycode
What is wrong with the following code..it does not work for me?
PHP Code:
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
function GetKeyCode() {
{
alert("KeyCode is " + document.event.KeyCode);
}
document.keydown=GetKeyCode();
}
</SCRIPT>
</HEAD>
<BODY onload="GetKeyCode()">
<P> Press the key you want to know the KeyCode
</BODY>
</HTML>
-
Mar 7th, 2006, 06:39 PM
#2
Junior Member
Re: keycode
Use this script:
<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>
you don't need a body onload function because the script will fire whenever a keypress is detected. One problem with your code is that when using document model functions, you do not use the parenthesis () when assigning a function to an event. So, you would use document.onkeydown = checkKeycode and NOT document.onkeydown = checkKeycode()
Another problem is the syntax you have used to capture the keycode is not correct. Both Explorer and Firefox capture differently, as shown in the code above in that there are two captures within if-then logic.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|