Hello everyone i just picked up jQuery
So my question is i want to stop a key from repeating when you hold it down
Example: Edit (i want it to stop .. so 1 character per keypress/keydown)
keypress: a
HoldDown: aaaaaaaaaaaaaaaaaaaaaaaaaaaaa
My code is:
<input type='text'>
var stop= false;
var time =null;
$(':text').keypress(function(e) {
if(stop === true){e.preventDefault();}
if (time !== null) {
lastkey = time;
}
time = new Date().getTime();
var between = time - lastkey;
if (between <= 70) {
stop = true;
}
});
$(':text').keyup(function(){stop = false;});
Am i going about this the wrong way?
Use the .keyup()
bind instead of .keypress()
.
Try this: http://jsfiddle.net/maniator/yFA8c/
It will only allow a letter per every 2 seconds.
Code:
var mult = false,
prev = 0;
$('.mult').keydown(function(e) {
if (!mult) {
mult = true;
prev = e.which;
setTimeout(function() {
mult = false;
}, 2000)
}
else if (prev != e.which) {
mult = false;
}
else {
return false;
}
});
You should use keydown and keyup events instead keypress.
So, here is a solution:
window.keypressed = {};
$('#element').keydown(function(e){
if ( window.keypressed[e.which] ) {
e.preventDefault();
} else {
window.keypressed[e.which] = true;
}).keyup(function(e){
window.keypressed[e.which] = false;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With