0

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!!!

samrat luitel
  • 53
  • 1
  • 8

1 Answers1

2

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.

Community
  • 1
  • 1
  • i know sleep command. i think you did not understand my question. i want such program which will exit the loop automatically after a certain time or until a key is press. i dont want program that wait for 4 second and start the loop. suppose if i press a then program will stop or else the program will stop in 5 second. Thank you for trying to answer – samrat luitel May 13 '16 at 10:40
  • @samratluitel See my edit. If you're just detecting a key press, you won't need to use a loop with a timer since `SLEEP` will suffice. However, I've added information regarding the usage of timer-based loops. I hope it helps to clarify things for you. –  May 13 '16 at 21:25
  • you are not getting my question. For simple clarification, i will tell you the program i am trying to make. suppose there is a letter "C". i want that "C" to move continuously forward as in the game like temple run but it is 2d. and whenever i press "w" key then that "C" will move one step upward or one column up. So make me such a program in which a letter will be continuously running in the row and whenever user press w it will move one step upward. Remember i dont want "C" to move one step forward in one second. It should continuously move forward in a fast pace – samrat luitel May 14 '16 at 17:02
  • @samratluitel Okay, I think I understand your requirements now. This isn't something that is easily done because you're trying to do two things at once: update the screen and read a key press. Basically, you run into the "Do work" and "detect key press" problem I mentioned if you keep them separate. What you want isn't to read a key press before a specified amount of time; you want to read a key press while still performing screen updates. If you use only QBASIC code, you'll have a lot of `IF UCASE$(INKEY$) = "W" THEN Jump` in your drawing code. –  May 14 '16 at 17:28
  • I will mention again that QB64's `ON TIMER(n)` would let you check with a resolution of milliseconds. For example, `ON TIMER(.100) GOSUB CheckKeyPress` would check for a key press every 100 milliseconds; you'd use the `RETURN` statement to return program execution to where it was after checking the key press of course, which would be your drawing code. If you are using QB64 instead of QBasic or QuickBASIC, you could take advantage of this. –  May 14 '16 at 17:32
  • can you made be a little clear about on timer go sub syntax i did not grab the concept. Thanks for answering – samrat luitel May 15 '16 at 14:47