Disable Cut/Copy/Paste for TMP_InputField

I need to be able to disable an input field from responding to cut/copy/paste, as I need to be able to process the pasted string prior to inserting it into the field. I tried using onValidateInput(), but the validation seems to occur after a paste, so I never see something like CTRL-V to reject, I only see the actual characters that are pasted in. Is there a ‘correct’ way to prevent the user from pasting/cutting, or to intercept and replace the value being pasted?

bump

Can’t seem to find any solutions online and this was the first result, so here is a janky workaround for preventing pasting on a TMP input field. You can adapt it similarly for cut/copy operations.

Partial credit to OP here: WebGL + TMP = copy paste issues

using TMPro;
using UnityEngine;

[RequireComponent(typeof(TMP_InputField))]
public class InputPreventPaste : MonoBehaviour
{
    private TMP_InputField _input;

    private string _lastInput = "";

    private void Awake()
    {
        _input = GetComponent<TMP_InputField>();
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.V)
            &&
            (Input.GetKey(KeyCode.LeftCommand)
                || Input.GetKey(KeyCode.RightCommand)
                || Input.GetKey(KeyCode.LeftControl)
                || Input.GetKey(KeyCode.RightControl)))
        {
            _input.text = _lastInput;
        }

        _lastInput = _input.text;
    }
}
1 Like