samrat luitel
samrat luitel

Reputation: 53

qbasic how to exit the loop after a specified time?

cls

do

keyed$=inkey$

loop until keyed$<>""

print"the hello world"

end

so in this program as you have seen until a press a key in the statement "keyed$=inkey$" the program wont display the hello world. so i want such a statement or program which will exit the loop in certain time. so the program will wait for 4 second and if users press the key before than that it will go to next line and even if the user wont press any key then program will move to next line in 4 second. please help me!!!

Upvotes: 0

Views: 1901

Answers (1)

user539810
user539810

Reputation:

You can use the SLEEP statement:

PRINT "Press a key within 4 seconds to win!"

' Wait 4 seconds or until a key is pressed.
SLEEP 4

' Collect the key pressed, if any.
keyed$ = INKEY$

IF keyed$ <> "" THEN
    PRINT "You WON by typing: "; keyed$
ELSE
    PRINT "You LOST!"
END IF

Note that certain keys such as the arrow keys are considered "extended" keys. The first character of keyed$ will be equal to CHR$(0) to tell you that an extended key was detected, and the second character will allow you to determine which key it was. You can find more information on the QB64 wiki about those "two byte codes" if you need to handle them. For such keys, the code above won't work so well, as shown below when I press the Up arrow key (CHR$(0) + CHR$(&H48)):

Press a key within 4 seconds to win!
You WON by typing:  H
                   ^note the extra space for CHR$(0)

Edit

You can use this instead of a loop to do what you want:

' Wait 4 seconds for a key to be pressed.
SLEEP 4

' If a key was not pressed in 4 seconds, keyed$ = "".
keyed$ = INKEY$
IF keyed$ = "" THEN PRINT "Timeout (no key pressed)"

PRINT "the hello world"

In other words, no loop is needed to detect whether a key was pressed if you use the SLEEP statement and immediately use the INKEY$ function afterward.

If you still prefer a timer-based solution with a loop, then you can use a few extra variables, an additional loop condition, and the TIMER function, which returns the number of seconds elapsed since midnight. The following does the same thing as the SLEEP method above:

maxWait = 4
startTime = TIMER
DO
    ' Detect a key press.
    keyed$ = INKEY$

    ' Fix the start time if the loop started before midnight
    ' and the current time is after midnight.
    IF startTime > TIMER THEN startTime = startTime - 86400

' Loop until a key is pressed or maxWait seconds have elapsed.
LOOP UNTIL keyed$ <> "" OR startTime + maxWait < TIMER

' If a key was not pressed in 4 seconds, keyed$ = "".
IF keyed$ = "" THEN PRINT "Timeout (no key pressed)"

PRINT "the hello world"

The advantage to this more complex option is the fact that you can do other things in the loop while waiting for a key press. Of course, there is a slight problem. If you're doing too much, the key press will be detected late because INKEY$ won't be called right when the key is pressed. For example:

maxWait = 4
startTime = TIMER
DO
    ' Do some work.
    x = 0
    FOR i = 1 TO 1000000
        x = x + 1
    NEXT i

    ' Detect a key press and exit the loop if one is detected.
    keyed$ = INKEY$

    ' Fix the start time if the loop started before midnight
    ' and the current time is after midnight.
    IF startTime > TIMER THEN startTime = startTime - 86400

' Loop until a key is pressed or maxWait seconds have elapsed.
LOOP UNTIL keyed$ <> "" OR startTime + maxWait < TIMER

It's tricky to avoid the issue. One option is ON TIMER(n) as suggested by @MatthewWhited, except you can only use one timer event. This means you would need it to exit the loop after maxWait seconds rather than detect a key press, even if there is work occurring. Meanwhile, your key presses will still be detected noticeably later than desired. QB64 allows for the usage of multiple timers, allowing you to handle both. It also allows you to specify n with millisecond precision, unlike QBasic that only allows for 1-second precision at minimum. See ON TIMER(n) on the QB64 Wiki for more information about QB64's improvements to ON TIMER(n).

Since I don't know your usage case, I can't recommend one solution or another, based on the code you've provided.

Upvotes: 2

Related Questions