Page 1 of 1

?????

Posted: Thu Mar 13, 2008 8:06 pm
by Yarrow
What code would you use to do an action if you pressed one of the keys? I looked though the tutorials but coudn't find it. :(

Posted: Thu Mar 13, 2008 9:49 pm
by Mac
You test the key and do whatever you want

Here is an example that prompts until you
press "Q"

Code: Select all

CLS
DO
  PRINT "Press key: ";
  DO: k$ = INKEY$: LOOP WHILE k$ = ""
  PRINT k$
  IF UCASE$(k$) = "Q" THEN END
LOOP


Posted: Fri Mar 14, 2008 3:18 am
by k7
My way:

Code: Select all

CLS 
DO 
  PRINT "Press key: "; 
  DO: k$ = INKEY$: LOOP WHILE k$ = "" 
  PRINT k$ 
LOOP UNTIL UCASE$(k$) = "Q"
END

Posted: Fri Mar 14, 2008 6:08 am
by Mac
k7 wrote:My way:
Yeah, in this case that is better. I would normally do that, too.

Here I was trying to show how to catch a key so it could be used for whatever, not just exit.

Mac

Hmmmmmmm

Posted: Fri Mar 14, 2008 11:57 pm
by burger2227
Mac you both used END instead of SYSTEM too. Bad boys LOL

Ted

Re: Hmmmmmmm

Posted: Sat Mar 15, 2008 12:21 am
by Mac
burger2227 wrote:Mac you both used END instead of SYSTEM too. Bad boys LOL

Ted
Good catch, but I had a reason: focus on how to capture key, not how to terminate a program.

Code: Select all

CLS
PRINT "Press ESC to exit program, 'T' to print time"
DO
  PRINT : PRINT "Press key: ";
  DO: k$ = INKEY$: LOOP WHILE k$ = ""
  IF k$ = CHR$(27) THEN EXIT DO ELSE PRINT k$
  IF UCASE$(k$) = "T" THEN PRINT TIME$
LOOP
PRINT : PRINT
LINE INPUT "Press ENTER"; e$
SYSTEM
Shows that programs that are to be compiled need to end with some kind of final prompt so the program doesn't leave before you see final results.

Also shows that ESC is a good way out rather than bogus Q.

Mac

Posted: Sat Mar 15, 2008 9:16 pm
by Yarrow
Thanks guys!

Posted: Sat Mar 15, 2008 11:08 pm
by k7
Guess I've got no excuse for my mistake. I've been programming in Creative BASIC lately, very similar to QB/FB in console mode.