Press any Key

BlitzPlus Forums/BlitzPlus Programming/Press any Key

Grey Alien(Posted 2005) [#1]
I want to ask the user to press any key before the program continues and have written this.

;some code ...
ccWaitForKeyOrMouse
flip
;some more code
ccWaitForKeyOrMouse
flip

Function ccAnyKeyPressed()
	Local TheKey = 0
	TheKey = GetKey()
	;erase arrow keys already held down as flush keys doesn't do it!
	If TheKey >= 63232 And TheKey <= 63235 Then TheKey = 0
	Return TheKey
End Function

Function ccWaitForKeyOrMouse()
	;Flushing twice is important to clear out before and after
	ccFlushAll()
	While ccAnyKeyPressed() = 0 And GetMouse() = 0	
	Wend
	ccFlushAll()
End Function

Function ccFlushAll()
	FlushKeys
	FlushMouse
End Function


ccWaitForKeyOrMouse() works fine *once* BUT, it you call it again, say on the next screen and the user has held the key down without releasing it, it will immediately say "yep a key has been pressed" and not wait at all. Which strictly is true due to the key repeating. FlushKeys doesn't work because it only kills what's currently in the buffer and as soon as the next key comes through due to key repeat, the user is allowed to progress.

I have got round this before by saying press SPACE to continue and using KeyDown(57) by tracking when it released and only letting them progress if they released the key before pressing it again. But there is no way to do this with ALL keys except for writing a stupid for loop to test all keys.

So in summary can anyone think of a decent way to allow the user to press any key to progress but one which ignores key repeats due to it being held down so the user is forced to lift their finger and press again? Hope I've made this clear.

Thanks in advance


VIP3R(Posted 2005) [#2]
Hmm, actually that's quite tricky as GetKey() returns 0 between the key repeats.

I'll have a think and see if I can come up with something for you ;)


Grey Alien(Posted 2005) [#3]
Exactly. Sounds cool, look forward to seeing something!


Ross C(Posted 2005) [#4]
Do you need to use getkey()?. Can't you use keydown()?


VIP3R(Posted 2005) [#5]
I can't see any solution to this using GetKey() alone GA. It's the wrong way to do it tbh.

Here's how I would do it...
Graphics 640,480,0,2

WaitForKeyOrMouse()
Text 0,0,"1st Press"
Flip

WaitForKeyOrMouse()
Text 0,40,"2nd Press"
Flip

Delay 2000



Function WaitForKeyOrMouse()

FlushKeys()
FlushMouse()

Local CheckKey=0

While CheckKey=0 And GetMouse()=0

	For Count=1 To 88
		CheckKey=CheckKey+KeyDown(Count)
	Next

Wend

End Function

Note: It doesn't check for the arrow keys, same as your example.


Grey Alien(Posted 2005) [#6]
Thanks VIP3R, you basically confirmed what I thought. Thus my quote "except for writing a stupid for loop to test all keys.". Seems like the for loop is the only option.


Arem(Posted 2005) [#7]
Have you tried the waitkey command?


VIP3R(Posted 2005) [#8]
WaitKey() detects the key repeats too, same problem as GetKey() in this case.


Grey Alien(Posted 2005) [#9]
Thanks Ross and Arem, but I've tried them all and know their various strengths and weaknesses which I why I started this thread. For example WaitKey pauses until a key is pressed, this is not good if you want to wait for anykey to be pressed in a loop while you animate a screen for example.


thalamus(Posted 2005) [#10]
How about a While...Wend Loop?


Grey Alien(Posted 2005) [#11]
I'm already using one.