Rapid Key Pressing

Blitz3D Forums/Blitz3D Beginners Area/Rapid Key Pressing

Tyler(Posted 2003) [#1]
How would one go about stopping the rapid key-press syndrome? I've got two camera modes (First Person and Third Person) I'm switching between them by pressing TAB. The problem is, I'm switching between them faster than the speed of reason and want to make it a one press = one switch deal. Could someone point me in the right direction? Thanks.


Binary_Moon(Posted 2003) [#2]
use keyhit rather than keydown :)


ErikT(Posted 2003) [#3]
Or the er, hard way if you want to keep track of pressed and held keys:

If KeyDown(TAB) ; where TAB is a constant holding
; the scancode for tab
If TAB_Held = 0 ; cumulative variable
Switch_CameraMode() ; Your camera switching stuff
EndIf
TAB_Held = TAB_Held + 1
Else ; reset TAB_Held when tab's not pressed
TAB_Held = 0
Endif


jhocking(Posted 2003) [#4]
Why use the hard way when KeyHit will do nicely? Note with KeyHit: you should only check it once per frame. With KeyDown it is no problem to check it as many times as you want but KeyHit should only be checked once every time through the game loop.


Tyler(Posted 2003) [#5]
Many thanks :)


WolRon(Posted 2003) [#6]
Why use the hard way when KeyHit will do nicely? Note with KeyHit: you should only check it once per frame. With KeyDown it is no problem to check it as many times as you want but KeyHit should only be checked once every time through the game loop.


You answered your own question. You can only check the state of the key once per loop with keyhit AND there is a possibility that if you check keydown more than once per loop, it might not be in the same state that it was earlier within the same loop (which, depending on how your code is written, might really bugger things up).

So, by doing it the "Hard Way", you can check the state of the key (TAB_held) as many times as you want per loop with no ill effects.


Binary_Moon(Posted 2003) [#7]
erm... but the easier way to check repeatedly would be


escapehit=keyhit(1)

if escapehit
   ;... do stuff
endif

;... something here to justify having two ifs

if escapehit
   ;... do more stuff
endif


That's easier than the code above isn't it?

Also the code above could be simplified to

If KeyDown(TAB) and Tab_held=false
   Switch_CameraMode()
   TAB_Held = true 
Else
   TAB_Held = false
Endif



WolRon(Posted 2003) [#8]
escapehit=keyhit(1)

if escapehit
;... do stuff
endif

;... something here to justify having two ifs

if escapehit
;... do more stuff
endif


Exactly what I am talking about.
So Erik Thon's code should look like this:
;beginning of loop
tabhit=keyhit(15)

if tabhit
   ;... do stuff
endif

;... something here to justify having two ifs

if tabhit
   ;... do more stuff
endif
;end of loop