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?

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

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);
}

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;
    }
}