If any 3 keyhits() end program

Blitz3D Forums/Blitz3D Beginners Area/If any 3 keyhits() end program

jigga619(Posted 2010) [#1]
In my game, I need the code in which after ANY 3 keyhits on the keyboard, the program ends.



I know the keyhit(28) or whatever command, but how do I code it so after 3 hits, it end the program? Do I use the <>0 with the keyhit command?


dawlane(Posted 2010) [#2]
I would think that the best way to handle this would be to use the GETKEY() function along with a variable to hold the number of times a key has been pressed along with some sort of timer. Here's a quickie
ref_count = 0	
timer = 0		

terminate = 0

While Not terminate
	
	Cls
	If GetKey() <> 0 
		If timer > 0 Then ref_count = ref_count + 1
		If timer < 1
			timer = 200
			ref_count = ref_count + 1
		EndIf
	EndIf
	
	If timer > 0 Then timer = timer - 1
	If timer = 0 Then ref_count = 0
	If ref_count > 2 Then terminate = True
	
	Text 0,0,"timer " + timer
	Text 0,20,"ref counter " + ref_count
	Flip
	
Wend

You could possibly use the MilliSecs() function to do the timer.
Edit: You don't have to use a timer if your not bothered about having to hit the keys in quick succession. You could just do this
terminate = 0
ref_count = 0

While Not terminate
	If GetKey() <> 0 Then ref_count = ref_count + 1
	If ref_count > 2 Then terminate = True
Wend



Dabhand(Posted 2010) [#3]
Erm, cheap and cheerful, check scancodes from 1-102, which is the meat of a keyboard really:-

Graphics 640,480

Const KEY_ESCAPE% = 1
Local counter = 0

Repeat
Cls
For loop = 1 To 102
	If KeyHit(loop) = True
		counter = counter + 1
		If counter = 3 Then End
		Exit 
	End If
Next

Text 0,0,"Count "+counter
Flip
Until KeyDown(KEY_ESCAPE)


Just another way really! :)

Dabz