Keydown event fire once without a flag

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:
  1.   var i=0;
  2.   var flag=true;
  3.   $(document).keydown(function(e){
  4.     var theKeyCode=(e.keyCode || e.which);
  5.     if (flag==true){
  6.       $("#counter").text(i+=1);
  7.       flag=false;
  8.     }
  9.   });
  10.  
  11.   $(document).keyup(function(e){
  12.     var theKeyCode=(e.keyCode || e.which);
  13.     flag=true;
  14.   });