Qbasicnews.com

Full Version: Continue by hitting a certain key
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
A simple question:
The SLEEP command stops the application until the user pushes any key, but how do I do to force the user to hit a certain key (eg space) to let him continue running the program?

Thanks in advance!
SLEEP 6000


Hehehe!

I don't know...


WHILE Inkey$=""
WEND

:normal:
Code:
DO: LOOP UNTIL INKEY$=" "
Quote:
Code:
DO: LOOP UNTIL INKEY$=" "

Kanoners! tack Tongue
Code:
Do
khb$ = inkey$
'your code goes here
loop untill khb$ = "g"

You can replace g with any other letter, number or character.

Or if you want to use a key like escape then you use the ascii code chr$(27). where 27 is the code for ESC.
You can view a list of all the ascii character codes, using the qbasic help. In the contents, there is a link to them.

Code:
Do
KHB$ = inkey$
'your code here
loop until inkey$ = chr$(27)

Also you could use

Code:
DO
khb$ = inkey$
'your code here
if inkey$ = "g" or inkey$ = chr$(27) then goto 100
loop
100
'more code

note: the "or inkey$ = chr$(27)", is not necessary.
try to avoid GOTO. Better use EXIT DO in this example (the last one).
You should use a library. Unless you enjoy POUNDING on your keyboard.
In this case I personally (although my opinion is really inferior compared to some other people here) would use something like;

99
INPUT " "; yourvariablehere
IF yourvariablehere$ = "key you want them to hit here" THEN
GOTO 1 '(Place a one in front of you continued program)
END IF
BEEP
PRINT "Invalid Answer"
SLEEP 5
GOTO 99

And that would tell the person that their answer was incorrect. By using this system you can also make other keys register certain commands; such as to just exit the program altogether, or stop it for modifications. Its all up to you. However, when using the goto and input commands together, it is importaint not to get caught in an infinate loop.
So, yeah. There are some cases when the GOTO command is not evil. You just have to know how to harness its evil power. Smile.
When told to "Goto Hell" a programmer believes the instruction is worse than the destination
Quote:When told to "Goto Hell" a programmer believes the instruction is worse than the destination
Good joke, I'm assuming you're new to this forum, but not to programming? Tongue
Pages: 1 2 3