?????

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Yarrow
Newbie
Posts: 5
Joined: Fri Feb 22, 2008 10:24 am
Location: U.S.A

?????

Post by Yarrow » Thu Mar 13, 2008 8:06 pm

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. :(
Opera omnia ... or so I wish!
(Opera omnia means all is well)

Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Post by Mac » Thu Mar 13, 2008 9:49 pm

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


User avatar
k7
Coder
Posts: 41
Joined: Wed Aug 01, 2007 7:38 am
Location: Tasmania, Australia
Contact:

Post by k7 » Fri Mar 14, 2008 3:18 am

My way:

Code: Select all

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

Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Post by Mac » Fri Mar 14, 2008 6:08 am

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

User avatar
burger2227
Veteran
Posts: 2356
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Hmmmmmmm

Post by burger2227 » Fri Mar 14, 2008 11:57 pm

Mac you both used END instead of SYSTEM too. Bad boys LOL

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Re: Hmmmmmmm

Post by Mac » Sat Mar 15, 2008 12:21 am

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

Yarrow
Newbie
Posts: 5
Joined: Fri Feb 22, 2008 10:24 am
Location: U.S.A

Post by Yarrow » Sat Mar 15, 2008 9:16 pm

Thanks guys!
Opera omnia ... or so I wish!
(Opera omnia means all is well)

User avatar
k7
Coder
Posts: 41
Joined: Wed Aug 01, 2007 7:38 am
Location: Tasmania, Australia
Contact:

Post by k7 » Sat Mar 15, 2008 11:08 pm

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.

Post Reply