1

I'm using a TMP input field and would like to override some default behaviour. Specifically, when a letter is held down, it repeats. Similarly, when backspace (or delete) is held down, it removes multiple characters.

I would like it so that if a letter is held down, that letter is only entered once. I would also like backspace to only remove one character if it is held down.

I am already implementing Input.GetKeyDown() to perform a certain task, so I believe I will need to do something similar here, but I can't quite work out how.

Thanks.


EDIT:

Thanks @daniel-m. This works sometimes, but other times it allows for 1 or 2 repeating letters.

I added extra Debug.Logs to investigate, like so:

private void Update()
    {
        if (IsFieldSelected)
        {
            // if a key is pressed
            if (Input.anyKeyDown)
            {
                // save the the current text to lastText (this key will have been entered)
                lastText = inputField.text;
                Debug.Log("Key was pressed! inputField = " + inputField.text + ", inputField = " + lastText);
            }

            // if a key is held down
            if (Input.anyKey)
            {
                Debug.Log("Key held down, part 1! inputField = " + inputField.text + ", inputField = " + lastText);
                inputField.text = lastText;
                Debug.Log("Key held down, part 2! inputField = " + inputField.text + ", inputField = " + lastText);
            }
        }
    }

And I held down the keys the keys 1 to 6 consecutively with the following Keydown and Keyup timestamps (recorded in a different script):

key logs and time stamps

The output into the text box was 1122334456. You can see in the debug logs below that a repetition is added to the inputField.text. However, it doesn't always show up in the input field - numbers 1-4 repeated but 5 and 6 didn't. And when the numbers do repeat, they don't show up in the input field until the key is released (even though the debug log says it's already in there).

debug logs

E_Williams
  • 31
  • 5
  • `Specifically, when a letter is held down, it repeats. Similarly, when backspace (or delete) is held down, it removes multiple characters.` this is related to your input device .. it is the normal behavior for any PC keyboard in any program – derHugo Sep 06 '21 at 16:36

1 Answers1

2

I have tested a little bit around and I think I have a solution for your problem.
When a key is pressed I save the current text into lastText. If a key is held down I set the current text inside the input field to lastText. So the code essentially sets the text field to the last text that was saved.

public class Text : MonoBehaviour
{
    public TMP_InputField inputField;
    private bool IsFieldSelected;
    private string lastText;

    private void Update()
    {
        if (IsFieldSelected)
        {
            if (Input.anyKeyDown)
            {
                lastText = inputField.text;
            }

            if (Input.anyKey)
            {
                Debug.Log(inputField.text);
                inputField.text = lastText;
            }
        }
    }

    public void IsSelected()
    {
        IsFieldSelected = true;
    }

    public void IsNotSelected()
    {
        IsFieldSelected = false;
    }
}

These are the events inside the InputField (TMP) gameobject. enter image description here

Daniel M
  • 746
  • 5
  • 13
  • Thanks Daniel, I thought this worked, but I've edited my answer with an issue I've found. (Sorry for the delay in testing out your suggestion, many thanks for offering a solution!) – E_Williams Sep 17 '21 at 09:36