- Home /
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?
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
This doesn't work. Inherited InputField does not react on typing inside. I can't find out why.
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);
}
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;
}
}
Your answer
Follow this Question
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