View Full Version : event.keyCode

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.


<body onKeyPress='JumpKey()'>

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

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()'>

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

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

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">
<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}
//END on document
<input type="text" onkeypress="capturekey(event)">