开发者

AS3 TextField - unwanted carriage return when setting value to ""

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 va开发者_运维知识库lue 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 = "";
    }
}


Simply replace KEY_DOWN with KEY_UP. It fixes the problem (for me at least)


Use KEY_DOWN -> Your code -> KEY_UP -> empty Field

An example (nasty code):

Message.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void
                     {
                        if(e.keyCode == Keyboard.ENTER)
                        {
                            // Your code...
                        }
                     });
Message.addEventListener(KeyboardEvent.KEY_UP, function(e:KeyboardEvent):void
                         {
                             if(e.keyCode == Keyboard.ENTER)
                             {
                                 Message.text = "";
                             }
                         });


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?


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.


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.


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);
}
}


I know it is a very old question, still thought of sharing my solution. I disabled multiline feature of textbox in fla file. After that I don't get any carriage return in textbox.


This does seem to be some kind of bug - I get it too. If you just set textField.text = "" in your constructor (or where-ever) it seems to resolve the problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜