Qbasicnews.com

Full Version: INKEY$
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hey guys it is me again, I was just wondering if it was possible to make it so it did something to the affect of:

IF INKEY$ = "anykey" THEN GOTO 1

or something like that. I know that "anykey" is not a valid parameter for the INKEY statement, but, I was wondering if someone knew how to do it anyway. I know how to make it so that if you push q or enter then it goes to 1 but is there a way to make it so that if a user pushes any key on the entire keyboard that it will go? Any help would be appreciated

(it's probably in that manual, but whatever Wink )
*cries again*
[Image: bawling.gif]

IF INKEY$ <> "" THEN GOTO 1

That's ONE of the N^999 solutions to your question!





*looks hopeless to his shotgun*
Yeah. That's how it works: INKEY$ is just like a string variable where the system puts a string which consists on the keys that are in the buffer. If no key has been pressed, the buffer is empty and INKEY$="". When a key is pressed, whatever the key is (except CTRL, SHIFT and such) copied in ascii form to the INKEY$ variable. That way, INKEY$=something or, what it's the same INKEY$<>"".

Anyways, it is better to clear the buffer before. Y'know, imagine a long loop in your program. If you press a key, as it is not being read, it will remain in a buffer. If you have an IF INKEY$<>"" THEN ... sentence right after the buffer, that key will be read by INKEY$ and maybe that's not what you like. So it is better to do this:

Code:
WHILE INKEY$<>"":WEND   ' loops while the buffer is full.
WHILE INKEY$="":WEND    ' waits for a new keypress.

Note the loops: The first one loops while there are keys pressed. That cleans the buffer. Then, the second one, loops while there aren't keys pressed, so it will exit when you press a key. Note that this is only aplicable when you are just waiting for a keypress, and not if you want that keypress to exit a loop, like for example in the challenge screensaver snippets, where you have to use HD's sollution.
SOR-EE guys jeepurs at least Na_th_an was nice enough to give me a straight out answer and not cry about it.

No but seriously, Sorry for acting like a total newbie (with a swelled ego) because I THOUGHT I was a good programmer but I guess (after seeing you guys) I am really just a beginner (to intermediate) one.................. So, thanks be to you all ye programmers who know more than I, for thee hast shown me the true errors of mine ways. :lol:

Seeya guys later and thans for thine help
Well, first of all: I answered you question, then I got depressed, 'cuz I just saw at that time your RTFM moment and I remembered your self-confidence and your so-called "intermediate skills". Sorry if I affected your enormous ego, but you know, the bigger the things are, the more difficult to miss. [Image: diablotin.gif]
WHAHAHA

I suck and I know it
Just discovered na_th_an's bit of code to empty the buffer and it was just what I was looking for. Many thanx man !

while inkey$ <> "":wend

Simple but effective
Compare the length, rather than the string. It's faster.

For example,
Code:
DO WHILE LEN(INKEY$) = 0: LOOP
even faster:

Code:
WHILE LEN(INKEY$):WEND
WHILE NOT LEN(INKEY$):WEND
The fastest way to clear the keybuffer (but you cant read from it when you do this)

Code:
DEF SEG=0: POKE &H41A, PEEK(&H41C)
DEF SEG
(Arghh,,,, why does a line not end in ;?? To much C coding Wink)

You can also read from the keyboard by this:

Code:
keycode = INP(&H60)

And if you go to the code post, you can have pure qb multikey!
Pages: 1 2