get user's input but only for 5 seconds

Discuss any general programming issues here
Post Reply
peceha
Posts: 111
Joined: Tue Dec 13, 2016 9:39 am
Location: Poland

get user's input but only for 5 seconds

Post by peceha » Tue Feb 07, 2017 9:58 am

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.

xabierpayet
Posts: 160
Joined: Fri Feb 24, 2012 10:34 am

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

Post by xabierpayet » Wed Feb 08, 2017 12:15 am

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

peceha
Posts: 111
Joined: Tue Dec 13, 2016 9:39 am
Location: Poland

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

Post by peceha » Wed Feb 08, 2017 12:57 am

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.

xabierpayet
Posts: 160
Joined: Fri Feb 24, 2012 10:34 am

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

Post by xabierpayet » Wed Feb 08, 2017 3:15 am

i dont understand very well your purpose, but i think that is better use checkevent() in this case

peceha
Posts: 111
Joined: Tue Dec 13, 2016 9:39 am
Location: Poland

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

Post by peceha » Wed Feb 08, 2017 9:14 am

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.

xabierpayet
Posts: 160
Joined: Fri Feb 24, 2012 10:34 am

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

Post by xabierpayet » Wed Feb 08, 2017 5:26 pm

i think that checkevent() will be better than waitevent() for do this, you can control the time exactly with checkevent

peceha
Posts: 111
Joined: Tue Dec 13, 2016 9:39 am
Location: Poland

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

Post by peceha » Wed Feb 08, 2017 8:43 pm

You are right! CHECKEVENT works perfectly.

Thank You !

Post Reply