Highlighted

Replace injected text in flash.text.TextField

Engaged ,
Feb 21, 2018

Copy link to clipboard

Copied

Hello,

using TextEvent.INPUT_EVENT, is-it possible to replace the injected text in the flash.text.TextField ?

var textField:TextField = new TextField();

textField.type = TextFieldType.INPUT;

textField.width = 200;

textField.height = 20;

textField.background = true;

textField.border = true;

textField.addEventListener(TextEvent.TEXT_INPUT, textInputHandler);

this.addChild(textField);

function textInputHandler(event:TextEvent):void

{

    event.text = "v";

}

When I put "a" in the keyboard, "v" is write in the textField instead of "a".

Thanks

I extended flash.text.TextField for this

Main.as

package

{

    import com.text.TextField;

    import flash.display.Sprite;

    import flash.events.Event;

    import flash.events.TextEvent;

   

    public class Main extends Sprite

    {

        private var textField:TextField = new TextField();

       

        public function Main() {

            this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);

        }

       

        private function addedToStageHandler(e:Event):void

        {

            this.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);

           

            textField.border = true;

            textField.type = "input";

            textField.wordWrap = true;

            textField.multiline = true;

            textField.addEventListener(TextEvent.TEXT_INPUT, textInputHandler, false, 1);

            textField.addEventListener(Event.CHANGE, textChangeHandler);

            this.addChild(textField);

        }

       

        private function textInputHandler(event:TextEvent):void

        {

            if(event.text == "a") event.text = "v";

        }

       

        private function textChangeHandler(event:Event):void

        {

            trace(textField.text);

        }

   

    }

}

com.text.TextField.as

package com.text

{

    import flash.events.Event;

    import flash.events.TextEvent;

    import flash.text.TextField;

   

    public class TextField extends flash.text.TextField

    {

        private var textEvent:TextEvent;

        private var newTextEvent:TextEvent;

       

        public function TextField()

        {

            super();

           

            this.addEventListener(TextEvent.TEXT_INPUT, textInputHandler, false, 2);

            this.addEventListener(TextEvent.TEXT_INPUT, newTextInputHandler);

            this.addEventListener(Event.CHANGE, textChangeHandler, false, 1);

        }

       

        private function textInputHandler(event:TextEvent):void

        {

            textEvent = new TextEvent(TextEvent.TEXT_INPUT, false, false, event.text);

        }

       

        private function newTextInputHandler(event:TextEvent):void

        {

            newTextEvent = new TextEvent(TextEvent.TEXT_INPUT, false, false, event.text);

        }

       

        private function textChangeHandler(event:Event):void

        {

            if( textEvent.text == newTextEvent.text ) return;

            var newCaretIndex:int = this.caretIndex - ( textEvent.text.length - newTextEvent.text.length );

            this.replaceText( caretIndex - textEvent.text.length, caretIndex, newTextEvent.text );

            this.setSelection(newCaretIndex, newCaretIndex);

        }

    }

}

TOPICS
Development

Views

319

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Replace injected text in flash.text.TextField

Engaged ,
Feb 21, 2018

Copy link to clipboard

Copied

Hello,

using TextEvent.INPUT_EVENT, is-it possible to replace the injected text in the flash.text.TextField ?

var textField:TextField = new TextField();

textField.type = TextFieldType.INPUT;

textField.width = 200;

textField.height = 20;

textField.background = true;

textField.border = true;

textField.addEventListener(TextEvent.TEXT_INPUT, textInputHandler);

this.addChild(textField);

function textInputHandler(event:TextEvent):void

{

    event.text = "v";

}

When I put "a" in the keyboard, "v" is write in the textField instead of "a".

Thanks

I extended flash.text.TextField for this

Main.as

package

{

    import com.text.TextField;

    import flash.display.Sprite;

    import flash.events.Event;

    import flash.events.TextEvent;

   

    public class Main extends Sprite

    {

        private var textField:TextField = new TextField();

       

        public function Main() {

            this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);

        }

       

        private function addedToStageHandler(e:Event):void

        {

            this.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);

           

            textField.border = true;

            textField.type = "input";

            textField.wordWrap = true;

            textField.multiline = true;

            textField.addEventListener(TextEvent.TEXT_INPUT, textInputHandler, false, 1);

            textField.addEventListener(Event.CHANGE, textChangeHandler);

            this.addChild(textField);

        }

       

        private function textInputHandler(event:TextEvent):void

        {

            if(event.text == "a") event.text = "v";

        }

       

        private function textChangeHandler(event:Event):void

        {

            trace(textField.text);

        }

   

    }

}

com.text.TextField.as

package com.text

{

    import flash.events.Event;

    import flash.events.TextEvent;

    import flash.text.TextField;

   

    public class TextField extends flash.text.TextField

    {

        private var textEvent:TextEvent;

        private var newTextEvent:TextEvent;

       

        public function TextField()

        {

            super();

           

            this.addEventListener(TextEvent.TEXT_INPUT, textInputHandler, false, 2);

            this.addEventListener(TextEvent.TEXT_INPUT, newTextInputHandler);

            this.addEventListener(Event.CHANGE, textChangeHandler, false, 1);

        }

       

        private function textInputHandler(event:TextEvent):void

        {

            textEvent = new TextEvent(TextEvent.TEXT_INPUT, false, false, event.text);

        }

       

        private function newTextInputHandler(event:TextEvent):void

        {

            newTextEvent = new TextEvent(TextEvent.TEXT_INPUT, false, false, event.text);

        }

       

        private function textChangeHandler(event:Event):void

        {

            if( textEvent.text == newTextEvent.text ) return;

            var newCaretIndex:int = this.caretIndex - ( textEvent.text.length - newTextEvent.text.length );

            this.replaceText( caretIndex - textEvent.text.length, caretIndex, newTextEvent.text );

            this.setSelection(newCaretIndex, newCaretIndex);

        }

    }

}

TOPICS
Development

Views

320

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 21, 2018 0
Enthusiast ,
Feb 22, 2018

Copy link to clipboard

Copied

Hi.

You are trying to change the event instead of TextField. You are also better off using the CHANGE event because the TEXT_INPUT event is fired before the letter is showing up in the input textfield. Try this code:

textField.addEventListener(Event.CHANGE, changeListener); 

function changeListener (e:Event):void

{

     textField.text = "v";

     // If you want each key stroke to produce a new "v", use this instead:

     //var s:String = textField.text;

     //textField.text = s.substr(0,s.length-1)+"v";

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 22, 2018 0
Engaged ,
Feb 24, 2018

Copy link to clipboard

Copied

I extended flash.text.TextField for this

Main.as

package

{

    import com.text.TextField;

    import flash.display.Sprite;

    import flash.events.Event;

    import flash.events.TextEvent;

   

    public class Main extends Sprite

    {

        private var textField:TextField = new TextField();

       

        public function Main() {

            this.addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);

        }

       

        private function addedToStageHandler(e:Event):void

        {

            this.removeEventListener(Event.ADDED_TO_STAGE, addedToStageHandler);

           

            textField.border = true;

            textField.type = "input";

            textField.wordWrap = true;

            textField.multiline = true;

            textField.addEventListener(TextEvent.TEXT_INPUT, textInputHandler, false, 1);

            textField.addEventListener(Event.CHANGE, textChangeHandler);

            this.addChild(textField);

        }

       

        private function textInputHandler(event:TextEvent):void

        {

            if(event.text == "a") event.text = "v";

        }

       

        private function textChangeHandler(event:Event):void

        {

            trace(textField.text);

        }

   

    }

}

com.text.TextField.as

package com.text

{

    import flash.events.Event;

    import flash.events.TextEvent;

    import flash.text.TextField;

   

    public class TextField extends flash.text.TextField

    {

        private var textEvent:TextEvent;

        private var newTextEvent:TextEvent;

       

        public function TextField()

        {

            super();

           

            this.addEventListener(TextEvent.TEXT_INPUT, textInputHandler, false, 2);

            this.addEventListener(TextEvent.TEXT_INPUT, newTextInputHandler);

            this.addEventListener(Event.CHANGE, textChangeHandler, false, 1);

        }

       

        private function textInputHandler(event:TextEvent):void

        {

            textEvent = new TextEvent(TextEvent.TEXT_INPUT, false, false, event.text);

        }

       

        private function newTextInputHandler(event:TextEvent):void

        {

            newTextEvent = new TextEvent(TextEvent.TEXT_INPUT, false, false, event.text);

        }

       

        private function textChangeHandler(event:Event):void

        {

            if( textEvent.text == newTextEvent.text ) return;

            var newCaretIndex:int = this.caretIndex - ( textEvent.text.length - newTextEvent.text.length );

            this.replaceText( caretIndex - textEvent.text.length, caretIndex, newTextEvent.text );

            this.setSelection(newCaretIndex, newCaretIndex);

        }

    }

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 24, 2018 0