help on limiting keys

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

guest123

help on limiting keys

Post by guest123 »

Why when I use this:
DO
SLEEP
LOOP UNITL INKEY$ = "w"
I get stuck in the loop even when i press "w".

What can I do to make it so that only certain keys will allow someone to continue?
Antoni
Veteran
Posts: 132
Joined: Wed Jun 15, 2005 3:01 pm
Contact:

Post by Antoni »

Code: Select all

 DO
SLEEP
LOOP UNITL lcase$(INKEY$) = "w" 
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt »

Also, ditch the SLEEP. The code will run fine (and better) without it.
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Antoni wrote:

Code: Select all

 DO
SLEEP
LOOP UNITL lcase$(INKEY$) = "w" 
That shouldnt be needed, and is not needed.
I have left this dump.
User avatar
Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

Try this

Post by Zim »

I use

Code: Select all

Do
k$=inkey$
loop until len(k$)
It pops out of the loop when you press anything. Then you can test what was pressed. For example, if k$=chr$(27) then the Esc key was pressed.
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---
Count Chocula
Newbie
Posts: 3
Joined: Wed Jan 18, 2006 10:34 pm
Location: Windsor, Ontario

Post by Count Chocula »

I've used

Code: Select all

DO
LOOP until INKEY$ <> ""
or

Code: Select all

DO
LOOP until INKEY$ = "w"
RayBritton
Veteran
Posts: 109
Joined: Thu Feb 10, 2005 12:38 pm

Post by RayBritton »

i mainly use

Code: Select all

do
key$=inkey$
loop until key$ <> ""
paulunknown
Coder
Posts: 17
Joined: Sat Aug 27, 2005 10:26 am

Post by paulunknown »

I tried

Code: Select all

DO
LOOP UNTIL INKEY$ = "w"
and it doesn't work.
paulunknown
Coder
Posts: 17
Joined: Sat Aug 27, 2005 10:26 am

Post by paulunknown »

Nevermind, it works.
Thanks.
paulunknown
Coder
Posts: 17
Joined: Sat Aug 27, 2005 10:26 am

Post by paulunknown »

There's another problem though.

DO
LOOP UNTIL INKEY$ = "w"
SELECT CASE INKEY$
CASE IS = "w"
x =x -1
END SELECT

The select case part don't seem to work.
User avatar
The Awakened
Veteran
Posts: 144
Joined: Sun Aug 07, 2005 1:51 am

Post by The Awakened »

Well, for starters, CASE IS is not a command, it's just CASE.

And it doesn't matter what INKEY is after the loop. As soon as someone presses w, it exits the loop and you might as well just scrap the whole select case thing and go x = x + 1. Just do this:

Code: Select all

DO
LOOP UNTIL INKEY$ = "w"
x = x + 1

"Sorry for beating you up with a baseball bat Julian, but I DID think that you were a samsquanch."
paulunknown
Coder
Posts: 17
Joined: Sat Aug 27, 2005 10:26 am

Post by paulunknown »

I mean if I want to have more than one key. So then I would need SELECT CASE.
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

paulunknown wrote:There's another problem though.

Code: Select all

DO
LOOP UNTIL INKEY$ = "w"
SELECT CASE INKEY$
     CASE IS = "w"
      x =x -1
END SELECT
The select case part don't seem to work.
Think of the keyboard buffer as a queue..

Every time you call INKEY, the oldest key gets returned (the key that has spent the most time in the queue)

As an example, lets say the user press:
A, B, C

First char in would be A, now, the first time you call INKEY, it will return A.
However, second time you call it, it will return the next character in the queue, which is B in our example.

Fixed:

Code: Select all

DO
k$ = INKEY$
LOOP UNTIL k$ = "w"
SELECT CASE k$
[...]
I have left this dump.
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt »

Z!re wrote:

Code: Select all

DO
k$ = INKEY$
LOOP UNTIL k$ = "w"
SELECT CASE k$
[...]
You bonehead...that specific code is never gonna work. :D k$ is ALWAYS going to be w after the loop because the loop won't end until it is so your SELECT CASE will only ever work with CASE "w". :P Silly Swede... ;)
User avatar
Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

Post by Zim »

Think of the keyboard buffer as a queue..
Indeed!
I've used:

Code: Select all

For i=1 to 16
dummy=inkey$
next i
Just to make sure the keyboard buffer is EMPTY!
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---
User avatar
The Awakened
Veteran
Posts: 144
Joined: Sun Aug 07, 2005 1:51 am

Post by The Awakened »

edit: Yeah, code didn't work before. Here's something that does work:

Code: Select all


DO
    SELECT CASE INKEY$
         CASE "a"
              'do whatever
               EXIT DO
         CASE "b"
              'do whatever
               EXIT DO
     END SELECT
LOOP

Last edited by The Awakened on Tue Jan 24, 2006 2:57 pm, edited 1 time in total.
"Sorry for beating you up with a baseball bat Julian, but I DID think that you were a samsquanch."
User avatar
Zim
Veteran
Posts: 98
Joined: Mon Dec 05, 2005 4:31 pm
Location: Wisconsin, USA
Contact:

Sorry...

Post by Zim »

Sorry, Awakened, but that won't work. Inkey$ scans the keyboard one time when it's executed. The first keystroke will pop the program out of the first loop. The case structure will execute based upon the SECOND keystroke which will, in THIS case typically be blank or the null string.

EDIT: Yup, that's better...
Last edited by Zim on Wed Jan 25, 2006 12:49 pm, edited 1 time in total.
--- Zim ---
--- Time flies like an arrow, but fruit flies like a banana ---
Patz QuickBASIC Creations
Veteran
Posts: 399
Joined: Wed Mar 02, 2005 9:01 pm
Location: Nashville, Tennessee
Contact:

Post by Patz QuickBASIC Creations »

I use:

Code: Select all

Key$ = ""
WHILE Key$ = ""
Key$ = INKEY$
WEND
SELECT CASE LCASE$(Key$)   'LCASE$ is optional
CASE "w"
[. . .]
CASE "e"
[. . .]
END SELECT
Or, to test for other keys, you can use ASCII, as shown.

Code: Select all

SELECT CASE ASC(LCASE$(Key$))
CASE 27
[. . .] 'ESC was pressed.
END SELECT
They *should* work, but are untested.
paulunknown
Coder
Posts: 17
Joined: Sat Aug 27, 2005 10:26 am

Post by paulunknown »

I'm confused.
So what does work?
paulunknown
Coder
Posts: 17
Joined: Sat Aug 27, 2005 10:26 am

Post by paulunknown »

Nevermind. I just needed to use k$ = INKEY$ for it to work.
Post Reply