Search Unity

Copy TextField or TextArea text to Clipboard

Discussion in 'Scripting' started by Michael-Ryan, Jun 1, 2009.

  1. Michael-Ryan

    Michael-Ryan

    Joined:
    Apr 10, 2009
    Posts:
    184
    Is there a way to copy the selected text (or all text) of a TextField or TextArea to the system clipboard? I'd like to provide a GUI.Button with this functionality.

    Under OS X, I can select a portion of the TextArea text, and press Command-C to copy it to the OS clipboard, which I can then paste into an email or text editor.

    Is there any way to simulate the keyboard combo, or are there any built in function in Unity, Mono, or C# that would allow this functionality?

    It would be great if I could capture only the selected portion of the Text controls, although just grabbing everything would be okay at this point.
     
  2. Statement

    Statement

    Guest

    Well, you can definately put stuff into the clipboard with .NET, however, I don't know if you can do it in Mono. This code works for my C# project, but I can't seem to use it in Unity for some reason.

    Code (csharp):
    1. System.Windows.Clipboard.SetText("Sample paste");
    If you still want to dig deeper, here's a tutorial and a link to the documentation at msdn.

    Good luck, and if you can get it to work in Unity please reply to this thread, I'm curious :)
     
  3. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    I want to do something very similar, did you ever find a way to do this, even in U3 you don't seem to be able to import the clipboard class.
     
  4. andschaan

    andschaan

    Joined:
    Jun 26, 2008
    Posts:
    60
    Anyone now know how to copy to Clipboard in Mono?
     
  5. PaulAsh

    PaulAsh

    Joined:
    Nov 29, 2010
    Posts:
    108
    reviving an old thread,

    did anyone ever figure this out?
     
  6. vguruv

    vguruv

    Joined:
    Jul 8, 2008
    Posts:
    6
    I am also looking for a way to just get the selected text within a text area.
     
  7. God-at-play

    God-at-play

    Joined:
    Nov 3, 2006
    Posts:
    330
    I had to do a bit of digging to find this, and since this thread came up earlier in the search:
    EditorGUIUtility.systemCopyBuffer
     
  8. BrUnO-XaVIeR

    BrUnO-XaVIeR

    Joined:
    Dec 6, 2010
    Posts:
    1,687
    This week I started to mess around with EditorGUI code, learning how to build custom windows, etc.
    I managed to support proper copy/paste functionality to my custom window, but I can't find a way to make a copy of the current highlighted text, I can only copy the whole content of the textArea in such custom window.
    Does anyone knows if it's possible to copy only the highlighted text piece? I have no idea if it is possible, I can't do it.

    Edit: Unity's support says it is not possible to copy highlighted text in the Editor. sigh
     
    Last edited: Jan 11, 2012
  9. TheCheese

    TheCheese

    Joined:
    Nov 25, 2009
    Posts:
    82
    Unity REALLY needs to document this - there are a lot of questions on the forum about working in GUI textfields / textareas that can be answered with this bit of code:

    Code (csharp):
    1. function OnGUI(){
    2.  
    3.     // be sure to check if your textfield has keyboard focus first - -
    4.     var te : TextEditor = GUIUtility.GetStateObject((TextEditor), GUIUtility.keyboardControl);
    5.  
    6.     // the TextEditor class is really very nice - you can control selections, move the cursor and a whole lot more
    7.     if (te != null){
    8.         print(te.SelectedText);
    9.     }
    10. }
    TextEditor is a sweet class and gives you a ton of functionality - copying, pasting, moving words, controlling the cursor and selection, and a whole lot more. Unfortunately, there is NO documentation and you have to rely on autocomplete or reflection to figure out what your options are.
    So please, spread the word!
     
  10. Sat

    Sat

    Joined:
    Aug 2, 2012
    Posts:
    4
    Hello,
    I found two ways for setting text in the clipboard:
    Code (csharp):
    1.  
    2. Type T = typeof(GUIUtility);
    3. PropertyInfo systemCopyBufferProperty = T.GetProperty("systemCopyBuffer", BindingFlags.Static | BindingFlags.NonPublic);
    4. systemCopyBufferProperty.SetValue(null, "bla bla", null);
    5.  
    BUT, this first one does not work in web player... TheCheese's method is better, as it also works in webplayer mode (thx !).
    Code (csharp):
    1.  
    2. TextEditor te = new TextEditor();
    3. te.content = new GUIContent("bla bla");
    4. te.SelectAll();
    5. te.Copy();
    6.  
     
  11. RKSandswept

    RKSandswept

    Joined:
    Apr 26, 2013
    Posts:
    22
    Awesome, that worked!
     
  12. CrisV

    CrisV

    Joined:
    May 30, 2014
    Posts:
    8
    I haven't had any luck trying to get Sat's code to work on iOS. From the editor it seems to work exactly as wanted but when built onto an iPad the system/device clipboard doesn't seem to get updated. Any ideas?
     
  13. onevcat

    onevcat

    Joined:
    Jan 4, 2013
    Posts:
    105
    I just posted a cross-platform plugin to solve this mess thing. See it here in Asset Store, which could support iOS, Android, WP and some desktop platforms. It exposes the same API for all, and I hope it could make your life easier.
     
  14. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    Unfortunately as of version 4.6 you can no longer use Texteditor version as pasted above in the web player :(

    @onevcat does your solution support the web player? You seem to list everything but that one.
     
  15. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    ...And what about GUIUtility.systemCopyBuffer? Does that work reliably, even in the modern era of Unity.UI?

    (At the moment, I'm really looking for a way to get/set the clipboard in a web plugin app.)
     
  16. TalhaDX

    TalhaDX

    Joined:
    Feb 2, 2013
    Posts:
    94
    Is there a way to copy to clipboard in mobiles???
     
  17. monark

    monark

    Joined:
    May 2, 2008
    Posts:
    1,598
    You need to use a plugin from the asset store or roll your own
     
  18. syscrusher

    syscrusher

    Joined:
    Jul 4, 2015
    Posts:
    1,104
    Thanks much for this post. In Unity 5.3.4, the log indicates that "content" is obsolete. Instead of this:
    Code (csharp):
    1.  
    2. te.content = new GUIContent("blah blah");
    3.  
    one now uses this:
    Code (csharp):
    1.  
    2. te.text = "blah blah";
    3.  
    Other than that, the second method was exactly what I needed!
     
    cxode and senordiablo like this.
  19. senordiablo

    senordiablo

    Joined:
    Apr 3, 2013
    Posts:
    6
    thanks syscrusher super helpful
     
  20. Gadied

    Gadied

    Joined:
    Mar 7, 2013
    Posts:
    3
    Hello, how i can use this in my project?.... i need to atach to input gameobject?
     
  21. syscrusher

    syscrusher

    Joined:
    Jul 4, 2015
    Posts:
    1,104
    Can you ask the question more specifically, please? We need to understand the context of your question, that is, what you are trying to do in your code. Thanks.
     
  22. Seneral

    Seneral

    Joined:
    Jun 2, 2014
    Posts:
    1,206
    Hey, I applied the technique proposed here to a small snipped for use in UI:
    Code (csharp):
    1. /// <summary>
    2. /// Add copy-paste functionality to any text field
    3. /// Returns changed text or NULL.
    4. /// Usage: text = HandleCopyPaste (controlID) ?? text;
    5. /// </summary>
    6. public static string HandleCopyPaste(int controlID)
    7. {
    8.     if (controlID == GUIUtility.keyboardControl)
    9.     {
    10.         if (Event.current.type == EventType.KeyUp && (Event.current.modifiers == EventModifiers.Control || Event.current.modifiers == EventModifiers.Command))
    11.         {
    12.             if (Event.current.keyCode == KeyCode.C)
    13.             {
    14.                 Event.current.Use();
    15.                 TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
    16.                 editor.Copy();
    17.             }
    18.             else if (Event.current.keyCode == KeyCode.V)
    19.             {
    20.                 Event.current.Use();
    21.                 TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
    22.                 editor.Paste();
    23. #if UNITY_5_3_OR_NEWER || UNITY_5_3
    24.                 return editor.text;
    25. #else
    26.                 return editor.content.text;
    27. #endif
    28.             }
    29.             else if (Event.current.keyCode == KeyCode.A)
    30.             {
    31.                 Event.current.Use();
    32.                 TextEditor editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
    33.                 editor.SelectAll();
    34.             }
    35.         }
    36.     }
    37.     return null;
    38. }
    39.  
    Then just make a wrapper around your textfield (or any other keyboard control):
    Code (csharp):
    1. /// <summary>
    2. /// TextField with copy-paste support
    3. /// </summary>
    4. public static string TextField(string value, params GUILayoutOption[] options)
    5. {
    6.     int textFieldID = GUIUtility.GetControlID("TextField".GetHashCode(), FocusType.Keyboard) + 1;
    7.     if (textFieldID == 0)
    8.         return value;
    9.  
    10.     // Handle custom copy-paste
    11.     value = HandleCopyPaste(textFieldID) ?? value;
    12.  
    13.     return GUILayout.TextField(value, options);
    14. }
    Should theoretically work down to Unity 5.0 and on all platforms:)
    Hope that helps!
    Seneral
     
    Last edited: Dec 15, 2017
    God-at-play, BMRG14 and Novack like this.
  23. fwalkerCirca

    fwalkerCirca

    Joined:
    Apr 10, 2017
    Posts:
    57
    I believe line 13 should be:
    Code (CSharp):
    1.     return GUILayout.TextField(value, options);
     
    Seneral likes this.
  24. fwalkerCirca

    fwalkerCirca

    Joined:
    Apr 10, 2017
    Posts:
    57
    Are you able to get this to work on WebGL?
    The code seems to work but the text is always empty. As if the clipboard was returning empty.
    Anyone else having this issue?
     
  25. Seneral

    Seneral

    Joined:
    Jun 2, 2014
    Posts:
    1,206
    Thanks for the note, fixed:) Also added option to select all with 'Ctrl+A'.
    Hm, works for me in my Node Editor WebGL...
    Does it work on other platforms?
     
  26. fwalkerCirca

    fwalkerCirca

    Joined:
    Apr 10, 2017
    Posts:
    57
    So I found that I can copy and paste with in the WebGL app, From textarea to textarea or text fields. But not copy from outside the app. Say from a text file and into the Text Ares of the app :(
     
    lampartsygames likes this.
  27. dushyantbhatt

    dushyantbhatt

    Joined:
    Jun 14, 2016
    Posts:
    6
    Can i Copy -Paste Some text from My webGL app to clipboard like in Email or text editor,
    Or from email or notepad to my Text area in WebGl app. ?
    currently i'm just copy and paste into webGl app only not outside the app .
    Can anyone help on that ?
     
  28. codemaker2015

    codemaker2015

    Joined:
    Aug 19, 2018
    Posts:
    27

    1. Code (CSharp):
      1.  void CopyToClipboard(string s) {
      2.          TextEditor te = new TextEditor();
      3.          te.text = s;
      4.          te.SelectAll();
      5.          te.Copy();
      6. }
      7.   //Calling the method
      8.   CopyToClipboard("Hello World");
     
  29. HernandoNJ

    HernandoNJ

    Joined:
    May 13, 2018
    Posts:
    75