Skip to main content
added 178 characters in body
Source Link
Hellium
  • 2.9k
  • 1
  • 13
  • 27

I haven't tested the following script, but I guess you can detect whether the paste keys are pressed when the value of the input changes.

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour
{
    public UnityEngine.UI.InputField mainInputField;
    private string inputValue;

    public void Start()
    {
        inputValue = mainInputField.text;
        mainInputField.onValueChanged.AddListener(OnInputValueChanged);
    }

    // Invoked when the value of the text field changes.
    public void OnInputValueChanged(string newValue)
    {
        if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && (Input.GetKey(KeyCode.V) || Input.GetKeyUp(KeyCode.V)))
        {
            Debug.LogError("Not allowed");
            mainInputField.onValueChanged.RemoveListener(OnInputValueChanged);
            mainInputField.text = inputValue;
            mainInputField.onValueChanged.AddListener(OnInputValueChanged);
        }
        else
        {
            inputValue = newValue;mainInputField.text;
        }
    }
}

EDIT : After testing this code (which works), you will have as many "Not allowed" errors as you have characters in the copied string. My solution may not be the best I guess.

I haven't tested the following script, but I guess you can detect whether the paste keys are pressed when the value of the input changes.

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour
{
    public UnityEngine.UI.InputField mainInputField;
    private string inputValue;

    public void Start()
    {
        inputValue = mainInputField.text;
        mainInputField.onValueChanged.AddListener(OnInputValueChanged);
    }

    // Invoked when the value of the text field changes.
    public void OnInputValueChanged(string newValue)
    {
        if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && (Input.GetKey(KeyCode.V) || Input.GetKeyUp(KeyCode.V)))
        {
            Debug.LogError("Not allowed");
            mainInputField.onValueChanged.RemoveListener(OnInputValueChanged);
            mainInputField.text = inputValue;
            mainInputField.onValueChanged.AddListener(OnInputValueChanged);
        }
        else
        {
            inputValue = newValue;
        }
    }
}

I haven't tested the following script, but I guess you can detect whether the paste keys are pressed when the value of the input changes.

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour
{
    public UnityEngine.UI.InputField mainInputField;
    private string inputValue;

    public void Start()
    {
        inputValue = mainInputField.text;
        mainInputField.onValueChanged.AddListener(OnInputValueChanged);
    }

    // Invoked when the value of the text field changes.
    public void OnInputValueChanged(string newValue)
    {
        if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && (Input.GetKey(KeyCode.V) || Input.GetKeyUp(KeyCode.V)))
        {
            Debug.LogError("Not allowed");
            mainInputField.onValueChanged.RemoveListener(OnInputValueChanged);
            mainInputField.text = inputValue;
            mainInputField.onValueChanged.AddListener(OnInputValueChanged);
        }
        else
        {
            inputValue = mainInputField.text;
        }
    }
}

EDIT : After testing this code (which works), you will have as many "Not allowed" errors as you have characters in the copied string. My solution may not be the best I guess.

added 357 characters in body
Source Link
Hellium
  • 2.9k
  • 1
  • 13
  • 27

I haven't tested the following script, but I guess you can detect whether the paste keys are pressed when the value of the input changes.

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour
{
    public UnityEngine.UI.InputField mainInputField;
    private string inputValue;

    public void Start()
    {
        inputValue = mainInputField.text;
        mainInputField.onValueChangeonValueChanged.AddListener( OnInputValueChanged );
    }

    // Invoked when the value of the text field changes.
    public void OnInputValueChanged( string newValue )
    {
        if( ( (Input.GetKey( KeyCode.LeftControl ) || Input.GetKey( KeyCode.RightControl ) ) && ( Input.GetKey( KeyCode.V ) || Input.GetKeyUp( KeyCode.V )))
        {
            Debug.LogError("Not allowed");
            mainInputField.onValueChanged.RemoveListener(OnInputValueChanged);
            mainInputField.text = inputValueinputValue;
            mainInputField.onValueChanged.AddListener(OnInputValueChanged);
        }
        else
        {
            inputValue = newValuenewValue;
 ;       }
    }
}

I haven't tested the following script, but I guess you can detect whether the paste keys are pressed when the value of the input changes.

public class Example : MonoBehaviour
{
    public InputField mainInputField;
    private string inputValue;

    public void Start()
    {
        inputValue = mainInputField.text;
        mainInputField.onValueChange.AddListener( OnInputValueChanged );
    }

    // Invoked when the value of the text field changes.
    public void OnInputValueChanged( string newValue )
    {
        if( ( Input.GetKey( KeyCode.LeftControl ) || Input.GetKey( KeyCode.RightControl ) ) && ( Input.GetKey( KeyCode.V ) || Input.GetKeyUp( KeyCode.V ) ) )
            mainInputField.text = inputValue ;
        inputValue = newValue ;
    }
}

I haven't tested the following script, but I guess you can detect whether the paste keys are pressed when the value of the input changes.

using UnityEngine;
using System.Collections;

public class Example : MonoBehaviour
{
    public UnityEngine.UI.InputField mainInputField;
    private string inputValue;

    public void Start()
    {
        inputValue = mainInputField.text;
        mainInputField.onValueChanged.AddListener(OnInputValueChanged);
    }

    // Invoked when the value of the text field changes.
    public void OnInputValueChanged(string newValue)
    {
        if ((Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl)) && (Input.GetKey(KeyCode.V) || Input.GetKeyUp(KeyCode.V)))
        {
            Debug.LogError("Not allowed");
            mainInputField.onValueChanged.RemoveListener(OnInputValueChanged);
            mainInputField.text = inputValue;
            mainInputField.onValueChanged.AddListener(OnInputValueChanged);
        }
        else
        {
            inputValue = newValue;
        }
    }
}
Source Link
Hellium
  • 2.9k
  • 1
  • 13
  • 27

I haven't tested the following script, but I guess you can detect whether the paste keys are pressed when the value of the input changes.

public class Example : MonoBehaviour
{
    public InputField mainInputField;
    private string inputValue;

    public void Start()
    {
        inputValue = mainInputField.text;
        mainInputField.onValueChange.AddListener( OnInputValueChanged );
    }

    // Invoked when the value of the text field changes.
    public void OnInputValueChanged( string newValue )
    {
        if( ( Input.GetKey( KeyCode.LeftControl ) || Input.GetKey( KeyCode.RightControl ) ) && ( Input.GetKey( KeyCode.V ) || Input.GetKeyUp( KeyCode.V ) ) )
            mainInputField.text = inputValue ;
        inputValue = newValue ;
    }
}