I'm using .keydown() and .keyup() event handlers at the same time on a same object, jquery fires .keyup() whenever a key form the keyboard get released .. I want to prevent jquery from triggering .keyup() event until ALL keys get released (not only one) since I'm using multi-key-pressing functions ... any idea?
Asked
Active
Viewed 201 times
1 Answers
1
Store a count of the keydown events along with a count of the keyup events. For each iteration you will need to subtract the keyup count from the keydown count. When the count reaches 0 you will have had as many keyups as keydowns and you can then run your function.
EDIT: this is FAR from perfect, but demonstrates the concept http://jsfiddle.net/K2ny2/1/
function testKeys(u, d) {
var delta = u - d;
console.log(delta);
if(0 == delta) {
$('#result').show();
}
}
You might want to include some code to avoid the problem when someone will hold down the keys (repeats).
![](../../users/profiles/1011527.webp)
Jay Blanchard
- 32,731
- 15
- 70
- 112
-
I thought it works @Jay, unfortunately, it doesn't. For some reasons, when I press keydown Jquery keeps increasing kd (not only by one but as much as I keep holding the key) so when I release it the delta won't be equal to zero!! I have tracked it using console.log and look what I got: (keyup = 1, keydown =1, KeyUp-KeyDown = 0, keyup = 2, keydown =2, KeyUp-KeyDown = 0, keyup = 3, keydown =3 KeyUp-KeyDown = 0, keyup = 4, keydown =6 KeyUp-KeyDown = -2 , keyup = 5 , keydown =6 KeyUp-KeyDown = -1 ) try to go back to your demo and press&hold for awhile, then release them ... – Saajid Akram Apr 18 '14 at 20:51
-
I did point out that you're going to need something to avoid holding down the keys @SaajidAkram - read the last sentence of my answer. – Jay Blanchard Apr 22 '14 at 21:52