Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Sep 2002
    Location
    Charlotte
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Trying to get arrow key to work

    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>

  • #2
    New Coder
    Join Date
    Sep 2002
    Location
    Charlotte
    Posts
    60
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Got it

    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •