...

View Full Version : Trying to get arrow key to work



dan18088
09-29-2003, 12:16 AM
Any ideas on why the below code does not work? I don't get any errors but nothing happens.

<td><font size=-1><input name="Regular" onKeyDown="return KEY( this );" ID="Cell_990101" size="3" value="80"></TD>
<td><font size=-1><input name="PTO" onKeyDown="return KEY( this );" ID="Cell_990102" size="3"></TD>
<td><font size=-1><input name="Other" onKeyDown="return KEY( this );" ID="Cell_990103" size="3"></TD>
<td><font size=-1><input name="Other_Type" onKeyDown="return KEY( this );" ID="Cell_990104" size="3"></TD>
<td><font size=-1><input name="Reg_hours" onKeyDown="return KEY( this );" ID="Cell_990105" size="3"></TD>
</tr>
</table>


<SCRIPT Language="JavaScript">
var MAXROW = 1;
var MAXCOL = 5;

// the thing that does the work...
function KEY( cell )
{
var kc = event.keyCode;
// not one of the arrow keys? Do nothing
if ( kc < 37 || kc > 40 ) return true;
// for left and right arrows, leave meanings
// alone unless the CTRL key is also pressed
if ( ( kc == 37 || kc == 39 ) && ( ! event.ctrlKey ) ) return true;

// user pushed one of the arrow buttons...handle it...
//
// first, figure out what cell this is:
var cname = cell.id;
var row = parseInt( cname.substring( 7, 9 ) );
var col = parseInt( cname.substring( 9, 11 ) );
// alert( "row=" + row + ", col=" + col + ": ctrlKey is " + event.ctrlKey);
switch ( kc )
{
case 37:
// left arrow
if ( --col < 1 ) col = MAXCOL;
break;
case 38:
// up arrow
if ( --row < 1 ) row = MAXROW;
break;
case 39:
// right arrow
if ( ++col > MAXCOL ) col = 1;
break;
case 40:
// down arrow
if ( ++row > MAXROW ) row = 1
break;
}

// the 99 is a dummy prefix to make it easy to build
// a cell name from the row and column number:
cname = "Cell_" + ( 990000 + 100 * row + col );
document.getElementById(cname).focus();
return false; // this cancels the normal action of the key
}

</SCRIPT>

dan18088
09-29-2003, 12:26 AM
It was the left and right arrows that needed the ctrl key pressed to move. I took out that part of the code and all is well.

Thanks anyway,
Dan



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum