I want that when a key is pressed for a long time it is not typed multiple times.
ejemplo:
tecla tiempo resultado
a 3seg a
I made this code but it doesn't work, use all 3 events.
var texto=document.getElementById("texto");
var pulsado=false;
texto.addEventListener('keydown', function(keyboardEvent) {
if(pulsado) return false;
pulsado=true;
});
texto.addEventListener('keypress', function(keyboardEvent) {
if(!pulsado){
}
});
texto.addEventListener('keyup', function(keyboardEvent) {
pulsado=false;
});
<textarea id="texto"></textarea>
Instead of timing, we need to know when the user is repeating a key. To do this, we use the KeyboardEvent.repeat property , which returns true when the key was not lifted and held down, generating a successive keystroke.
Adapted to different versions of Internet Explorer
IE is still the exception to the rule (-how weird, right?). In this case, when we associate the event with
addEventListener()
, IE always returnsKeyboardEvent.repeat == false
. Instead, it returns the correct value when attachEvent is used ...But there's more, attachEvent was deprecated as of IE11, leaving no direct solution. So, to fix this second problem, we use the meta tag for X-UA-Compatible legacy mode :
<meta http-equiv="X-UA-Compatible" content="IE=10" />
.And, of course, we added some exceptions for keys that you normally want to repeat (backspace, del, arrows, home, etc).
* For IE 8 - it is necessary to use the Polyfill of Array.prototype.IndexOf().
Demo subida a un hosting gratuito
To solve this, what I do is check if each key is pressed, instead of just one, then I add the list of letters to an object, setting a property like
letras["f"]=true
, when the key is released, it is set tofalse
, which is necessary condition to be able to tighten it again.Since we were talking in the chat with @Mariano , he offered a solution to detect only letters and not special keys like Backspace . This means that since the word Backspace is 9 characters long and not exactly 1, it works normally, while letters work custom.
When pressing a key and if the length is 1:
If the key was previously pressed, avoid double typing,
If it is not marked as pressed, the letter is typed and then marked as pressed.
Here I leave you the example working, it may need some other touch up if you have any other requirement. Basically it's about using the keyboard events correctly taking into account which one is executed first:
Try with this append a little more code to be able to achieve the result.
It is worth mentioning that I use jquery and javascript
you don't need to use all three events, just reset the text-area so that it always goes back to how it was at the beginning. Something like that:
In the html you will only tell it to react when a key is pressed:
Now every time there are more letters in the text area they will not be shown, except for the last letter that was pressed
We create a variable and when the user presses a key the keydown function will verify the state of the variable, if it is it will
true
cancel the repetition, otherwise it will ignore and it will becometrue
the variable and when the user releases it this variable will becomefalse
.Here my example: