2012-01-16 33 views
2

我有一個問題是如何將選定或複製的文本粘貼到flash或as3中的舞臺或文本區域?我複製了文本,但對如何將文本粘貼到文本字段沒有任何意見。 幫助!如何在AS3中粘貼選定的文本

在此先感謝!

+0

你試過簡單的Ctrl V嗎? – 2012-01-16 09:09:32

+0

是的,但ctrl + v在閃存之外工作,如記事本,寫字板等。我想在舞臺或任何文本區域框中粘貼文本。 – 2012-01-16 09:10:56

回答

5

我已經得到了答案:

package 
{ 
    import flash.display.Sprite; 
    import flash.desktop.Clipboard; 
    import flash.desktop.ClipboardFormats; 
    import flash.desktop.ClipboardTransferMode; 
    import flash.events.*; 
    import flash.system.System; 

    public class ClipboardExample extends Sprite 
    { 
     public function ClipboardExample() 
     {  
      Clipboard.generalClipboard.clear(); 
      copyButton.addEventListener(MouseEvent.MOUSE_UP, copyText); 
     } 

     private function copyText(e:MouseEvent):void 
     { 
      Clipboard.generalClipboard.clear(); 
      System.setClipboard(myField.text); 
      messageField.text = "Copied!"; 
      stage.addEventListener(Event.PASTE,paste); //Ctrl+V on stage 
     }  

     private function paste(e:Event):void 
     { 
      if(Clipboard.generalClipboard.hasFormat(ClipboardFormats.TEXT_FORMAT)) 
      {     
      messageField.text = String(Clipboard.generalClipboard.getData(ClipboardFormats.TEXT_FORMAT)); 
      } 
     } 
    }  
} 
0

Did you mean myTextField.text = copiedTextVariable ;?

+0

我想在舞臺或任何文本區域框中粘貼文本。 – 2012-01-16 09:11:44

0

我認爲這是你在找什麼:

package { 
    import flash.display.Sprite; 
    import flash.events.MouseEvent; 
    import flash.system.System; 
    import flash.text.TextField; 
    import flash.text.TextFieldType; 

    public class SelectTextTest extends Sprite { 
     public function SelectTextTest() { 
      var tf:TextField = new TextField(); 
      tf.x = 0; 
      tf.y = 0; 
      tf.width = 200; 
      tf.height = 200; 
      addChild(tf); 
      tf.wordWrap = true; 
      tf.type = TextFieldType.INPUT; 
      tf.text = "This is the text. Try to select"; 
      tf.addEventListener(MouseEvent.CLICK, printCursorPosition); 
     } 

     private function printCursorPosition(event:MouseEvent):void { 
      var tf:TextField = TextField(event.target); 
      trace("caretIndex:", tf.caretIndex); 
      trace("selectionBeginIndex:", tf.selectionBeginIndex); 
      trace("selectionEndIndex:", tf.selectionEndIndex); 
      trace(tf.text.substring(tf.selectionBeginIndex, tf.selectionEndIndex)); 
      System.setClipboard(tf.text.substring(tf.selectionBeginIndex, tf.selectionEndIndex)); 
     } 
    } 
} 
+0

如何將複製的文本粘貼到文本區域? – 2012-01-16 09:19:31

0

自Flash Player 10,嵌入網站應用閃光僅僅能夠訪問到Clipboard.generalClipboard.getData()方法,當他們正在處理由用戶直接調度的PASTE事件時。

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/desktop/Clipboard.html

注意:爲了避免該異常的安全性,該事件的目標必須是應用程序的階段對象。 我沒有在任何文檔中找到這些信息,它來自我自己的經驗。希望它有效