Advertisement
rodrigolopezpeker

Copiar texto de un texto field con Enter

Apr 30th, 2014
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Code by rodrigolopezpeker (aka 7interactive™) on 1/29/14 12:40 PM.
  3.  */
  4. package labs {
  5. import flash.display.Sprite;
  6. import flash.events.TextEvent;
  7. import flash.text.TextField;
  8. import flash.text.TextFieldType;
  9. import flash.text.TextFormat;
  10.  
  11. [SWF(width="400", height="100", backgroundColor="#323232", frameRate="60")]
  12. public class LabMain extends Sprite {
  13.  
  14.     private var input_tf:TextField;
  15.     private var textarea_tf:TextField;
  16.  
  17.     public function LabMain() {
  18.         init();
  19.     }
  20.  
  21.     private function init():void {
  22.         input_tf = addTF(TextFieldType.INPUT, 0xffffff);
  23.         textarea_tf = addTF(TextFieldType.DYNAMIC, 0xdedede);
  24.         input_tf.defaultTextFormat = new TextFormat('Arial', 12, 0x565656);
  25.         textarea_tf.defaultTextFormat = new TextFormat('Arial', 12, 0x0);
  26.         textarea_tf.x = 200;
  27.         input_tf.addEventListener(TextEvent.TEXT_INPUT, handleTextChange);
  28.  
  29.         stage.focus = input_tf;
  30.     }
  31.  
  32.     private function addTF(p_type:String, p_bgColor:uint):TextField {
  33.         var tf:TextField = new TextField();
  34.         tf.border = true;
  35.         tf.type = p_type;
  36.         tf.width = 200;
  37.         tf.borderColor = 0x0;
  38.         tf.background = true;
  39.         tf.multiline = true;
  40.         tf.backgroundColor = p_bgColor;
  41.         addChild(tf);
  42.         return tf;
  43.     }
  44.  
  45.     private function handleTextChange(event:TextEvent):void {
  46.         var tf:TextField = event.currentTarget as TextField;
  47.         if (event.text == '\n') {
  48.             if (tf.selectedText) {
  49.                 event.preventDefault();
  50.                 sendText(tf.selectedText);
  51.             }
  52.         }
  53.     }
  54.  
  55.     private function sendText(p_text:String):void {
  56.         textarea_tf.text += p_text + '\n';
  57.         textarea_tf.scrollV = textarea_tf.maxScrollV;
  58.     }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement