Input.GetKey is too fast to delete characters

Hi,

I am creating a virtual keyboard where I have many Buttons and a Text component that I am going to modify by pressing the buttons.

In addition to assigning functions to buttons, I’ve also tied them to keyboard inputs. That is, when on the computer keyboard I press a key that is present on the virtual keyboard I created, I automatically press the button (I used Input.GetKeyDown in an update method for this)

So I can click the buttons correctly. However I also wanted to add the behavior of the backspace button (when held down) that all normal keyboards have. That is, I want that when the user holds down the backspace button, the characters are deleted from the text as long as the button is held down. So in this case I have to abandon the idea of Input.GetKeyDown and use Input.GetKey. The problem is, it’s too fast. If I also write a text of many characters, as soon as I hold down the backspace they are all deleted practically instantly.

Is there a scripted way to make font deletion smooth and consistent when I hold backspace?

Thanks!

PS. playing a bit I also found a bug, reported and recognized (maybe it is useful to know it if you use these functions) (https://issuetracker.unity3d.com/issues/keycodes-less-and-greater-are-not-being -registered-when-using-getkeydown)

GetKey is not “fast” or “slow”, it simply returns true during any frame that the button is held down.

If you want a behavior of something happening at some fixed interval, you can write that behavior. Just use a variable to keep track of how much time has elapsed, and don’t do that action again until the appropriate amount of time has elapsed.

You probably just want to create your own delete timer. So, when delete is pressed down, maybe delete the first char. Then start your timer as long as the key is held down, then in increments, delete a char.

@PraetorBlue was faster with a response!

You need to manage your own delays. In most UX input you have two separate values, the initial delay, and then the repeat rate, and this will probably be the behavior your users expect. So you can do something like this:

private float allowNextKeyboardInput = 0f; // If Time.time > this value, we can type a character
public float initialDelay = 0.5f; //seconds
public float repeatRate = 0.1f;

void Update() {
if (Input.GetKeyDown(x)) {
TypeLetter();
allowNextKeyboardInput = Time.time + initialDelay;
}
else if (Time.time > allowNextKeyboardInput && Input.GetKey(x)) {
TypeLetter();
allowNextKeyboardInput += repeatRate; // using += here, rather than setting to Time.time + repeatRate, will keep it looking consistent even if a frame hiccup occurs
}
else if (!Input.GetKey(x)) {
allowNextKeyboardInput = 0f; //reset the time when the key is released
}

Thank you all.

Yes you are right, certainly it is not him who is too fast but I who have to go slower, Thanks for the correction.

Thanks for the idea, I think by script you meant what he wrote after @StarManta

Thanks! I didn’t really know how to do it (I’ve never played with time). It should work fine, now I try it, thanks again!