Update: As seen in one of the comments, it seems TEXT_INPUT is not the way to go, as it is “Dispatched after a control value is modified, unlike the textInput event, which is dispatched before the value is modified” check out the API

Acording to the Adobe API In Flash, by default a text field’s property is usualy set to “dynamic”, however if you change this property to “input” using the TextFieldType class, then it is possible to save user input for use in other parts of your appication – This is mainly useful when creating forms, or any application that need the user to define a text value somewhere else in the system.

In this tutorials example, the following code creates an imput text field called myFirstTextBox. As the user enters the text, An event is triggered, we will call this inputEvent.
To capture the string of text entered, we need an event handler – we shall call this inputCapture, it will capture the string and assign it a variable – then Flash Player or AIR displays the new text in another text field, called OutputBox.

package
{
  import flash.display.Sprite;
  import flash.display.Stage;
  import flash.text.*;
  import flash.events.*;

  public class CaptureInput extends Sprite
  {
    private var myFirstTextBox:TextField = new TextField();
    private var OutputBox:TextField = new TextField();
    private var myText:String = "Type your text here.";

    public function CaptureUserInput()
    {
      captureText();
    }

    public function captureText():void
    {
      myFirstTextBox.type = TextFieldType.INPUT;
      myFirstTextBox.background = true;
      addChild(myFirstTextBox);
      myFirstTextBox.text = myText;
      myFirstTextBox.addEventListener(TextEvent.TEXT_INPUT, inputEventCapture);
    }

    public function inputEventCapture(event:TextEvent):void
    {
      var str:String = myFirstTextBox.text;
      createOutputBox(str);
    }

    public function createOutputBox(str:String):void
    {
      OutputBox.background = true;
      OutputBox.x = 200;
      addChild(OutputBox);
      OutputBox.text = str;
    }
  }
}
Be Sociable, Share!

10 Responses to “Capturing Text Input in Actionscript 3”

  • rachel huntNo Gravatar says:

    Hi there,

    I wonder if i could double check a couple of things with you on this? Have you tested this? I have been wanting to use the TEXT_INPUT event handler to get hold of the contents of my input text fields, but whenever I have tried to do so, I dont get the full string, instead everything but the last character entered as the TEXT_INPUT event seems to trigger as the key is being pressed and so outputs the state of the text before the key press. The same thing happens when I test using your code.

    As far as I can tell the only way to get the true content is to use key press or an enterframe that is triggered on focus of the textfield. But this is a pain and so I wanted to make sure that there wasnt something simple I was missing before I endeavoured on this task.

    thanks, rach.

  • adminNo Gravatar says:

    Rachel,

    Thanks for your comment.
    Firstly, I have to ask you if you are using AS3 as this is what this code is designed to work with. & Make sure you are importing the correct classes (these do seem trivial, but they need to be asked!)
    I have not specifically tested this code, However your problem seems very intriguing, and so am not sure as to the exact solution, however – Try checking out the Adobe API for text input and using key press may seem long winded, but it could be a workaround for the moment.
    Sorry I couldn’t be of much use immediately, I will look into it and let you know.

  • rachel huntNo Gravatar says:

    Hi there,

    Yeap, I am using as3 (id rather not use as2 ever again) and yes I am importing all the classes. I have found other people having the same issue as me as it seems to be a nuance of the event itself, but I just found it strange as all of adobes documentation suggests that it should work, however testing their example code comes out with the same results for me. Quite odd really :)

    thanks anyway.

  • rachel huntNo Gravatar says:

    Ok, found the answer. TEXT_INPUT is not the way to go. use Event.CHANGE instead…

    “Dispatched after a control value is modified, unlike the textInput event, which is dispatched before the value is modified”

    http://livedocs.adobe.com/flex/3/langref/flash/text/TextField.html#event:change

  • adminNo Gravatar says:

    Ah yes,
    Correct, Thank you for your contribution!
    take care.

  • wagsterNo Gravatar says:

    Why are you copying Adobe’s product documentation and passing it off as your own tutorial? I have already read the livedocs: http://min.im/3gh8zg

  • adminNo Gravatar says:

    If you read the Tutorial from the Top carefully, you would have seen that I do not take credit for this tutorial.

  • randyglandNo Gravatar says:

    hehe, i had the same problem, I was trying to get around it by doing _textInput.text + e.text in the event handler function but it didn’t recognise deletes so I had to have keyboard event handling backspace presses which deleted the last character from the second textfield and ended up in a hole heap of shit when someone decided to move the cursor back a bit and then delete text, or select all then delete.

    So good job on the tutorial and the comments! cheers! :)

  • YoungCodeNo Gravatar says:

    Is there a more simple way, such as storing input text, like a name, that can be used in other frames?

  • ariskaNo Gravatar says:

    thanks. ^_^

Leave a Reply

This site is using OpenAvatar based on

Subscribe
Follow me on Twitter
free counters
PayPal Donation
If you like this blog and the help it has given you, why not donate for this good cause? Click on the button below to pay Safely and Securely through PayPal.
Thanks.