10

I want to catch the enter key press when the user is filling an input text field in AS3. I think I have to do something like this:

    inputText.addEventListener(Event. ? , func);
    function func(e:Event):void{
       if(e. ? == "Enter"){
          doSomething();
       }
    }

But I can't find the best way to do this. By the way, the input text has a restriction:

inputText.restrict = "0-9";

Should I add the enter key to the restrictions?

inputText.restrict = "0-9\n";

Thanks in advance.

3 Answers 3

33

Corrected Answer

You can easily listen for keyboard events occurring when the textfield is focused.

Just add a KEY_DOWN event directly to the textfield, and then do whatever you want.

// get key presses only when the textfield is being edited
inputText.addEventListener(KeyboardEvent.KEY_DOWN,handler);
function handler(event:KeyboardEvent){

   // if the key is ENTER
   if(event.charCode == 13){

       // your code here
       doSomething();
   }
}

On a separate note, textfields also send other useful events:

  • When modified by the user (if its an input textfield) -- Event.CHANGE
  • When text is typed by the user (if its an input textfield) -- TextEvent.TEXT_INPUT
  • When a link is clicked (if its HTML text) -- TextEvent.LINK
  • When scrolled by the user (if its multiline and the contents don't fit) -- Event.SCROLL

Previous Wrong Answer:

I think that the only way to actually do what you want is a bit complex.

Basically, you can't get any event from the regular TextField that would be fired when the Enter key is pressed. You have to do work around...

One idea would be to listen the textfield for focus events. When it has focus, you add a listener to the stage for key board events and check if the pressed key is Enter, if so fire your action, else skip.

Sample code:

inputText.addEventListener(FocusEvent.FOCUS_IN,textInputHandler);
inputText.addEventListener(FocusEvent.FOCUS_OUT,textInputHandlerOut);

function textInputHandler(event:FocusEvent):void {
    //our textfield has focus, we want to listen for keyboard events.
    stage.addEventListener( KeyboardEvent.KEY_DOWN, keyDownHandler);
}

function textInputHandlerOut(event:FocusEvent):void {
    //our textfield lost focus, remove our listener.
    stage.removeEventListener( KeyboardEvent.KEY_DOWN, keyDownHandler);
}

function keyDownHandler( e:KeyboardEvent ):void{
    //a key was pressed, check if it was Enter => charCode 13.
    if( e.charCode == 13 ){
      //ok, enter was pressed. Do your thing.
      trace("We pressed enter, doSomething" )
    }
}
3
  • 1
    I used: inputText.addEventListener(KeyboardEvent.KEY_DOWN,handler); function handler(event:KeyboardEvent){ if(event.charCode == 13){ doSomething(); } } And it works. Thanks. Commented Feb 19, 2010 at 16:33
  • Please check your facts before publishing such nonsense, this question is over a year old, and over 4000 people have been misled into believing that such simple things are difficult. Commented Jan 7, 2011 at 16:53
  • This actually doesn't work on iPhone with AIR, you have to use Event. CHANGE on TextField, KeyboardEvent does not work. Also, when "return" is indicated on the soft keyboard the carriage return '\r' isn't always indicated by Event.CHANGE. I'm not sure there is a way to detect a carriage return reliably and consistently.
    – Joe
    Commented Sep 22, 2015 at 7:55
2

For those wondering how to handle an "Enter" key press with a TextInput field rather than a TextField, here's the simple process:

import fl.events.ComponentEvent

...

myListeningField.addEventListener(ComponentEvent.ENTER, listenerMethod);


function listenerMethod(e:Event):void{
    //DO SOME STUFF
}
0

Are you using a textfield, or a TextInput?

The TextInput dispatches a Enter-event, when the enter is pressed. More on this can be found at http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/fl/controls/TextInput.html#event:enter

I'm not sure how this works for a Textfield.

1
  • I use the Text tool, and then i set that the box is an Input Text. I tried to use inputText.enter = function (e:Object){ bla } but it throws Error #1056: Can't create property enter in flash.text.TextField. Thanks. Commented Feb 19, 2010 at 16:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.