Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
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
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

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
Add comment · 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
0

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
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 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
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.

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

Hover Over Input Field Before Inputting? 2 Answers

Prevent focus from leaving inputfield 0 Answers

How do I get Input Fields to work with a controller? 0 Answers

Can I disable the use of Input.GetKey(KeyCode("any key")) while an InputField is in use? 2 Answers

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


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