• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Auzette · Mar 07, 2011 at 11:39 PM · textfieldcopypaste

Stopping Copy/paste into Textfield.

Does anyone know of a way to stop a user from pasting text into a text field? We have a situation where we want users to type into a text field, but not paste into a text field.

The only way I could think of was to look at the text in a text field and if it changed by more than one character set it back to its prior text (but I don't know if it's possible to type fast enough that a user could theoretically have more than one character entered by the next time I draw the TextField and examine its contents. I'm hoping for something that would let me do this in a better way.

Thanks.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ben · Mar 09, 2011 at 09:54 AM

It will Work

using UnityEngine; using System.Collections; using System.Text.RegularExpressions;

public class RestrictCharsInTextField : MonoBehaviour {

public string text;

void OnGUI() { text = GUI.TextField(new Rect(100,100,100,50), text, 10); text = Regex.Replace(text, @"[^a-zA-Z0-9.@ ]", "");

   if(Event.current.command || Event.current.control){  // cmd - for Mac   control - for windows

     text = "";
     }

}

}

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Auzette · Apr 15, 2011 at 02:37 AM 0
Share

HUGE thank you. this worked great!

avatar image ben · Apr 15, 2011 at 06:33 AM 0
Share

always welcome!

avatar image HHameline · Aug 16, 2011 at 06:07 PM 0
Share

Question: could someone still macro around this?

avatar image striche · Jun 25, 2012 at 04:47 PM 0
Share

This won't stop using Shift-Insert key combination on Windows.

avatar image Rupe-CS · Dec 03, 2013 at 03:29 PM 0
Share

We use the Event.current.command check but also cache the string and revert it if its length has increased by more than 1, to catch the Shift-Insert case.

In our case, the call's being made frequently enough for this to still allow fast typing, including repeated chars by holding a key down, as queried in the OP. But I guess that could vary, you'd have to try it.

avatar image
0

Answer by Statement · Mar 07, 2011 at 11:49 PM

Well, a brittle idea is to cause some sort of invalidation upon CTRL down (for CTRL + V shortcut). I don't know if it is feasible, but you could perhaps disable the text field for a second or so if that button is pressed, and clean out any value entered.

Or if it is possible, just set the text to string.Empty when Event.current.control == true. I haven't tried this myself, you'll have to check if it works or not.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by yoyo · Mar 08, 2011 at 05:24 AM

This almost works (C#) ...

string noPaste = "";
void OnGUI()
{
    string temp = GUILayout.TextField(noPaste);
    if (!Event.current.control)
        noPaste = temp;
}

It prevents pasting, but the text cursor still moves when the paste is performed. Not sure how to fix that part.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Cannot copy, select or paste in TextField on Android 0 Answers

Copy text in TextField to windows clipboard 2 Answers

How do I keep a texture different on copied objects? 1 Answer

Why cant I copy anything I have in my Project Tab to the Hierarchy Tab or Scene? 1 Answer

login window with user name 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges