Keydown event fire once without a flag
Hi,
Let's say I have a page with a counter (with an initial value of zero) and nothing else.
Here's what I want to do:
1) I press and HOLD a key down
2) The counter goes to one
3) I let go of the key
4) The counter still reads one
5) I press and HOLD another key down
6) The counter goes to two
7) I let go of the key
8) The counter still reads two
Is this possible to do without a flag?
I'm need another way because my key event triggers will be complex. For instance, I press and hold a key down. This increases the counter by one. Then if I hold that key still down and then press another key and hold that down, this will also increase the counter by one.
Here's my code with a flag:
- var i=0;
- var flag=true;
- $(document).keydown(function(e){
- var theKeyCode=(e.keyCode || e.which);
- if (flag==true){
- $("#counter").text(i+=1);
- flag=false;
- }
- });
-
- $(document).keyup(function(e){
- var theKeyCode=(e.keyCode || e.which);
- flag=true;
- });