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):
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).