Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Copy/Paste inserting letters 'c' and 'v' in GUI.TextField

Discussion in 'Immediate Mode GUI (IMGUI)' started by Steven-Walker, Jan 22, 2014.

  1. Steven-Walker

    Steven-Walker

    Joined:
    Oct 27, 2010
    Posts:
    38
    I have a custom editor GUI that uses GUI.TextField. When editing this field, if I press command+c to copy (I'm on a Mac), the copy works, but it inserts the letter 'c' over the selected text, or appends 'v' after pasting. I've tried intercepting this by detecting when the command key is down, but if I block the event using Event.current.Use(), the copy-paste no longer works. I want copy-paste to work of course, just without adding any extra text.

    Anybody ever run into this or know how to fix it?
     
  2. Steven-Walker

    Steven-Walker

    Joined:
    Oct 27, 2010
    Posts:
    38
    I found a solution. I switched to using EditorGUI.TextField, which properly handles copy-paste in the editor. However, this presented another problem with the text field holding onto the last value each time. I was able to fix that by setting the following upon displaying the field:
    GUIUtility.keyboardControl = 0;
     
  3. dkozar

    dkozar

    Joined:
    Nov 30, 2009
    Posts:
    1,410
    Code (csharp):
    1. if (GUILayout.Button("Copy"))
    2. {
    3.     GUI.FocusControl("YourDummyTextFieldFocusId"); // focus out the text area
    4.     TextEditor te = new TextEditor { content = new GUIContent(_yourTextToCopy) };
    5.     te.SelectAll();
    6.     te.Copy();
    7. }