views:

618

answers:

4

I have an input TextField and have a KeyboardEvent.KEY_DOWN even listener on the stage to listen for the Keyboard.ENTER event. The event listener adds the entered text to an array or whatever, and then clears the TextField. The problem is that when the Enter key event fires and the TextField value is set to "", it leaves a carriage return in the TextField and the cursor positioned on the second line. WTF? I've been coding AS2 and AS3 for a LONG time and have never ran into this before. Am I losing my mind? Please help, people! :-)

Example:

var myTextArray:Array = new Array();
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);

function onKeyDown(e:KeyboardEvent):void{
    if(e.keyCode == Keyboard.ENTER){
        if(_inputText.text != null){
            myTextArray.push(_inputText.text);
    }
    _inputText.text = "";
    }
}
A: 

If you are able to enter a carriage return, it means the TextField is multiline, isn't?

Have you tried to empty your input text in a KeyboardEvent.KEY_UP event listener callback function?

hadrien
A: 

Are you hitting enter with text field in focus? In that case, it might be that onKeyDown is called before the text field's value is updated by the keyboard action. You set the textfield to empty string in the onKeyDown, and then flash adds a new line to it.

Add a text input event listener to the text field and add trace statements to both event handlers to know the order of events.

Amarghosh
A: 

Hi

If you are in Flash authoring mode (in the IDE), you need to uncheck Control -> Disable Keyboard Shortcuts while testing. Also try making a "cleanup" handler for KEY_UP.

mga
A: 

inputfield.addEventListener(Event.CHANGE, onChange);

private function onChange(e:Event):void { if ( inputfield.text.charCodeAt(inputfield.text.length - 1) == 13 ) { inputfield.text = inputfield.text.substr(0, inputfield.text.length - 1); } }

SaggioFilosofo