Page 1 of 1

get user's input but only for 5 seconds

Posted: Tue Feb 07, 2017 8:58 am
by peceha
Hi,
what I'd like to achieve is to allow somebody to write a something but give him only 5 seconds to do the writing - so after 5 seconds he cannot write anymore and what he wrote so far is passed to the program.

Thank You.

Re: get user's input but only for 5 seconds

Posted: Tue Feb 07, 2017 11:15 pm
by xabierpayet
use this

starttimer(1)
repeat
t=gettimer(1)
if t<5000 ;the 5 seconds for write
blahbla......
else
blahblah
endif
if t>20000 then starttimer(1); restart the timer again when the timer reach 20 seconds
forever

Re: get user's input but only for 5 seconds

Posted: Tue Feb 07, 2017 11:57 pm
by peceha
Thanks for your help.

I tried to use that code but have no luck. What I did first was:

Code: Select all

str$=""
StartTimer(1)
Repeat
	t=GetTimer(1)
	If t<5000 ;the 5 seconds for write
		str$ = InKeyStr(#ALPHABETICAL)
	Else
		Break
	EndIf
Forever
DebugPrint(str$)
but it is not good since INKEYSTR will block the program till ENTER is hit thus there is no time limit in theory.

Then:

Code: Select all

str$=""
Function p_HandlerFunc(msg)
	str$=str$..msg.key
	Cls
	TextOut(0,0,str$)
EndFunction

InstallEventHandler({onKeyDown = p_HandlerFunc})

StartTimer(1)
Repeat
	t=GetTimer(1)
	If t<5000 ;the 5 seconds for write
		WaitEvent
	Else
		Break
	EndIf
Forever
DebugPrint(str$)
which is better but still no perfect - in that version program will not break the REPEAT-FOREVER loop unless I hit a key after 5 seconds. But it is better than first one since the first keystroke after 5 seconds will quit the writing.

Re: get user's input but only for 5 seconds

Posted: Wed Feb 08, 2017 2:15 am
by xabierpayet
i dont understand very well your purpose, but i think that is better use checkevent() in this case

Re: get user's input but only for 5 seconds

Posted: Wed Feb 08, 2017 8:14 am
by peceha
So the whole story looks like this:
you are in the dungeon, if you decide to attack somebody (or somebody attack you) the fight starts.
To win the fight, all you have to do is to write a correct translation of the word which shows on the screen after fight starts - for example you see english word "simultaneously" and you have to write polish "jednoczesnie" - but to make it more difficult there is a time limit for writing (5 seconds) so after that period program closes the "writing window", compares what you wrote to the correct translation and decides if you won or not.

Re: get user's input but only for 5 seconds

Posted: Wed Feb 08, 2017 4:26 pm
by xabierpayet
i think that checkevent() will be better than waitevent() for do this, you can control the time exactly with checkevent

Re: get user's input but only for 5 seconds

Posted: Wed Feb 08, 2017 7:43 pm
by peceha
You are right! CHECKEVENT works perfectly.

Thank You !