• 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
Question by Shnayzr · Aug 27, 2017 at 09:37 PM · uiinputinputfield

How can I block Copy Paste from Input Field?

I dont want the user to write a text by copying and then pasting, how can I do that?

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image

Answer by Bunny83 · Aug 27, 2017 at 10:10 PM

I guess you use the new UI system and we talk about an InputField? Well, in this case it's kinda difficult because the detection of the "paste" key combination is more or less hardcoded in the component.

One way that should work is to create a subclass of the InputField component and override the Append method that takes a string. As far as i can tell it's only used to inside the systemcopybuffer. Any normal input is processed by the Append method that takes a single char.

So this should be enough:

 // InputField_NoInsert.cs
 using UnityEngine;
 using UnityEngine.UI
 
 public class InputField_NoInsert : InputField
 {
     protected override void Append(string input)
     {
         // just do nothing
     }
 }


Now all you have to do is replacing the InputField component on your input gameobject with your own inputfield

Comment

People who like this

0 Show 1 · 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 Avelblood · Mar 12, 2018 at 11:14 PM 0
Share

This doesn't work. Inherited InputField does not react on typing inside. I can't find out why.

avatar image

Answer by $$anonymous$$ · Aug 27, 2017 at 10:14 PM

There are a couple ways to prevent copy and pasting in an input field by interrupting command events and such, if you really want to control input you can check for specific keys and store them in a string, this way the only way to input text would be to physically press the corresponding keys (or have some kind of 3rd party keyboard automation).

For example you can store all of your key codes in a list:

 private KeyCode[] keyCodes = {
         KeyCode.Alpha1,        KeyCode.Alpha2,        KeyCode.Alpha3,        KeyCode.Alpha4,
         KeyCode.Alpha5,        KeyCode.Alpha6,        KeyCode.Alpha7,        KeyCode.Alpha8,
         KeyCode.Alpha9,        KeyCode.Alpha0,
         KeyCode.Space,
         KeyCode.A,        KeyCode.B,        KeyCode.C,        KeyCode.D,
         KeyCode.E,        KeyCode.F,        KeyCode.G,        KeyCode.H,
         KeyCode.I,        KeyCode.J,        KeyCode.K,        KeyCode.L,
         KeyCode.M,        KeyCode.N,        KeyCode.O,        KeyCode.P,
         KeyCode.Q,        KeyCode.R,        KeyCode.S,        KeyCode.T,
         KeyCode.U,        KeyCode.V,        KeyCode.W,        KeyCode.X,
         KeyCode.Y,        KeyCode.Z,
     };

You could then iterate through them and add any inputs to a text component string in update:

 foreach (KeyCode kcode in keyCodes)
 {
     if (Input.GetKeyDown(kcode))
     {
         GetComponent<Text>().text += Input.inputString;
     }
 }

And if you need a backspace:

 if (Input.GetKeyDown(KeyCode.Backspace) && GetComponent<Text>().text.Length > 0)
 {
     GetComponent<Text>().text = GetComponent<Text>().text.Substring(0, GetComponent<Text>().text.Length - 1);
 }


Comment

People who like this

0 Show 0 · 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

Answer by Sneirox · Mar 23, 2021 at 10:37 AM

 using UnityEngine;
 using UnityEngine.UI;
 
 public class NoCopyPaste : MonoBehaviour
 {
     float time = 0f;
     string text;
     InputField input;
 
     void Start()
     {
         input = GetComponentInChildren<InputField>();
         input.onValueChanged.AddListener(delegate {OnInputValueChanged(); });
     }
 
     public void OnInputValueChanged()
     {
         if (Time.time - time < 0.01)
         {
             input.text = text;
         }
         else text = input.text;
         time = Time.time;
     }
 }

Comment

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

120 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

New gui: How to disable keyboard input when user is typing? 2 Answers

UI InputField text sometimes invisible? 1 Answer

Inputfield text to String variable 1 Answer

UI Get InputField value 0 Answers

How to make a position input field (like in the Inspector) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges