...

View Full Version : event.keyCode



peterinwa
08-12-2005, 03:14 PM
I am simply trying to capture and identify a keystroke, and the following gives the error message "object requried" in IE and "event not defined" in Netscape.

The problem is with var key=event.keyCode;

The keystroke is taking me to JumpKey() just fine.

Thanks.

<body onKeyPress='JumpKey()'>

// Function to accept keystroke
function JumpKey(){
var key=event.keyCode;
alert(key);
}

charlene
08-12-2005, 04:40 PM
Hey, I dont see why this wouldnt work, I tried it out, and works fine.
Here is my exact code just in case.

<body onKeyPress='JumpKey()'>

</body>
<script language="javascript">
function JumpKey(){
var key=event.keyCode;
alert(key);
}
</script>

peterinwa
08-12-2005, 05:02 PM
Thanks, got it working.

Having you tell me the code was good got me thinking a little harder. I'm using frames:

var key=parent.image.event.keyCode;

Fixed it.

Thanks, Peter

Kor
08-12-2005, 06:10 PM
here's a complete and crossbrowser code to capture the keycode (what you are using is IE only) on document or, at will, on a form's text field element, if needed:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Captura key code</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
//Genuine code by Corneliu Lucian 'KOR' Rusu -- mailto:corneliulucian@apropo.ro
function capturekey(e){//on text field
var key=(typeof event!='undefined')?window.event.keyCode:e.keyCode;
alert('keycode : '+key);
}
//on document
if(navigator.appName!= "Mozilla"){document.onkeypress=capturekey}
else{document.addEventListener("keyup",capturekey,true)}
//END on document
</script>
</head>
<body>
<input type="text" onkeypress="capturekey(event)">
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum