A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Use Return/Enter key to trigger function?

  1. #1
    Senior Member
    Join Date
    Aug 2000
    Location
    Central PA
    Posts
    120

    Use Return/Enter key to trigger function?

    I've got a little form in a movieclip where the student is to do some simple subtraction, then click the Record button to enter the data into a table. Problem is that if someone types in the answer, they naturally want to hit the Return key to enter the data, however, it instead puts a carriage return in their answer and when they try submitting with the Record button, it checks their answer and it is now wrong since the added carriage return does not match the answer they should have. Is there any way to make the Return key trigger the recordIt() function? Here is my code:

    recordBtn.addEventListener(MouseEvent.MOUSE_DOWN,r ecordIt);

    function recordIt(num) {
    if (diameter.text == "85.80") {
    if (difference.text == "1.10") {
    MovieClip(this.parent).growth.d1999.text = "85.80";
    MovieClip(this.parent).growth.g1999.text = "1.10";
    helpText.text = "Good job. Click the button below to do the next year's measurement.";
    recordBtn.visible = false;
    } else {
    helpText.text = "Incorrect result. Try again.";
    }
    } else {
    helpText.text = "Incorrect diameter. Try again.";
    }
    }
    ?:-{>

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    This is working for me but I'm concerned that it still adds the linebreak after this executes (which seems odd to me since the event wouldnt get processed until the next frame anyway)...it seems to work but you should give it some testing over different machines and browsers to make sure it's not catching the return in some cases. If you find that happening, you might set this listener to run in the capture phase to give it priority, or else you can get into the substring game and try to shave the return back off if you detect it.

    PHP Code:
    tf.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void{
        if(
    e.keyCode == Keyboard.ENTER){
            
    trace(tf.text);
        }
    }); 

  3. #3
    Senior Member
    Join Date
    Aug 2000
    Location
    Central PA
    Posts
    120
    thanks, neznein9. Here is what I ended up adding:

    stage.addEventListener(KeyboardEvent.KEY_DOWN,keyD ownListener);

    function keyDownListener(e:KeyboardEvent) {
    if ((e.keyCode == Keyboard.ENTER) && (diameter.text != "") && (difference.text != "")) {
    recordIt(e);
    }

    }

    Note: Looks like this web board is putting a space in the word "keyDownListener" above for some reason, so if you use this script, watch out.
    Last edited by pzb4; 07-29-2008 at 11:04 AM.
    ?:-{>

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center