SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Jul 2010
    0 Post(s)
    0 Thread(s)

    Function Onkeypress for cursor key

    Good day!

    In my javascript code I have a function for the enter key. I want to add cursor key in that function/ I research about the coding for cursor key but I can't find the code same on what I want to happen.

    Here is the code:
    <script language="javascript">
      function handleEnter(e, nextfield)
            var characterCode = (e && e.which)? e.which: e.keyCode;
            if(characterCode == 13)
                return false;
                return true;
    <input type='text' name='plt' onkeypress='return handleEnter(event,\"b0\");' />
    Thank you

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Melbourne, AU
    17 Post(s)
    1 Thread(s)
    There isn't any, but you can use the Unity function called GetMouseButtonDown(0). 1 is for the right button and 2 is for the middle button. There's a corresponding GetMouseButtonUp(0), as well as GetMouseButton(0), which returns true every frame as long as the mouse button is down.
    Alternately, you can set up a button in the input manager. That way you can make a mouse button, joystick button, gamepad button, keyboard key, etc. all do the same thing, and the button can be configured by the user when running a stand-alone build. Then you use GetButtonDown("NameOfButton")--depending on what you called the button in the input manager--as well as GetButtonUp and GetButton.

  3. #3
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    51 Post(s)
    2 Thread(s)
    What is cursor key? I don't have a key called that on my keyboard?

    I can't tell what your script wants to do. Who is document.getElementById(nextfield) ? some other element with the id of "b0"?

    You are also returning false. Is this to catch people using ENTER to submit the form without putting in any input? Yeah, I think browsers should force users to navigate focus to the submit button first, but it's too late: people are used to hitting enter, except when focussed on a textarea.

    If you are trying to make sure something's typed in, you could try something different (without regard to which key is used... because this is not the same per keyboard. You can maybe get away with US keyboards all having the same keycode, but beyond those... most of the command buttons can be different. For example the : key may be different between browsers).
    Read more about keycode and charcode trouble:

    First, you could say
    onblur(this), if this.value == ""
    this.focus or whatever.focus

    Though this should sufficiently annoy users : ) But if, when they leave the input, if it's empty, move the focus back to the same input (you could also try a return false in there too to stop submission).

    You could also try onchange or onkeyup, but this may hurt users who are backspacing a field empty so they can change their answer.

    Or you could just check all your inputs when the user tries to submit.

    document.forms['thisformID'].submit() {
    //check the inputs
    //if their values == ""
    //return false
    //return true (let the form submit)

    This would be faster if you have a lot of inputs, but it doesn't tell the user right away that something needs to be filled in.

    Best thing to do in all cases is explain (before/above the form) that inputs must be filled in first. Then use Javascript to catch them if they miss (esp if they are hitting ENTER by accident).


Posting Permissions

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