Page 1 of 2

help on limiting keys

Posted: Thu Jan 19, 2006 11:24 pm
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?

Posted: Fri Jan 20, 2006 9:27 am
by Antoni

Code: Select all

 DO
SLEEP
LOOP UNITL lcase$(INKEY$) = "w" 

Posted: Fri Jan 20, 2006 10:55 am
by Nodtveidt
Also, ditch the SLEEP. The code will run fine (and better) without it.

Posted: Fri Jan 20, 2006 11:13 am
by Z!re
Antoni wrote:

Code: Select all

 DO
SLEEP
LOOP UNITL lcase$(INKEY$) = "w" 
That shouldnt be needed, and is not needed.

Try this

Posted: Fri Jan 20, 2006 12:45 pm
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.

Posted: Fri Jan 20, 2006 3:59 pm
by Count Chocula
I've used

Code: Select all

DO
LOOP until INKEY$ <> ""
or

Code: Select all

DO
LOOP until INKEY$ = "w"

Posted: Sat Jan 21, 2006 3:18 am
by RayBritton
i mainly use

Code: Select all

do
key$=inkey$
loop until key$ <> ""

Posted: Sat Jan 21, 2006 12:49 pm
by paulunknown
I tried

Code: Select all

DO
LOOP UNTIL INKEY$ = "w"
and it doesn't work.

Posted: Sat Jan 21, 2006 12:53 pm
by paulunknown
Nevermind, it works.
Thanks.

Posted: Sat Jan 21, 2006 12:59 pm
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.

Posted: Sat Jan 21, 2006 2:25 pm
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


Posted: Sat Jan 21, 2006 2:56 pm
by paulunknown
I mean if I want to have more than one key. So then I would need SELECT CASE.

Posted: Sat Jan 21, 2006 3:44 pm
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$
[...]

Posted: Mon Jan 23, 2006 10:42 am
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... ;)

Posted: Mon Jan 23, 2006 12:51 pm
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!

Posted: Mon Jan 23, 2006 1:16 pm
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


Sorry...

Posted: Tue Jan 24, 2006 12:28 pm
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...

Posted: Tue Jan 24, 2006 3:12 pm
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.

Posted: Fri Jan 27, 2006 1:02 am
by paulunknown
I'm confused.
So what does work?

Posted: Fri Jan 27, 2006 1:27 am
by paulunknown
Nevermind. I just needed to use k$ = INKEY$ for it to work.