Qbasicnews.com

Full Version: General Problems
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hey, it's me again! I have a somewhat lesser and unimportant problem that in no way will have a big problem in the final program, as I have an alternative way that works. So here's the lowdown on what I would like this section of the program to do: I would like the program to display the menu with one of the selections highlighted in blue. When you press an arrow key, in order to bring up the menu, the menu comes up with the first option highlighted as it should be. However from that point on when I try to use the arrow keys to change the selection it simply remains the same screen!

Here is the code:
Code:
Menu1: WHILE INKEY$ = ""
WEND
Key$ = INKEY$
IF Key$ = CHR$(0) + CHR$(72) THEN LET menu% = menu% - 1
IF menu% = 0 THEN LET menu% = 1
IF Key$ = CHR$(0) + CHR$(80) THEN LET menu% = menu% + 1
IF menu% = 6 THEN LET menu% = 5
    
IF Key$ = CHR$(0) + CHR$(77) THEN GOTO selection

CLS
COLOR 4, 0, 0
PRINT "Spanish Helper Version 1.1"
COLOR 7, 0, 0
IF menu% = 1 THEN COLOR 7, 1, 0 ELSE COLOR 7, 0, 0
PRINT "1) Origin"
IF menu% = 2 THEN COLOR 7, 1, 0 ELSE COLOR 7, 0, 0
PRINT "2) Continue"
IF menu% = 3 THEN COLOR 7, 1, 0 ELSE COLOR 7, 0, 0
PRINT "3) Data wipe"
IF menu% = 4 THEN COLOR 7, 1, 0 ELSE COLOR 7, 0, 0
PRINT "4) Reference"
IF menu% = 5 THEN COLOR 7, 1, 0 ELSE COLOR 7, 0, 0
PRINT "5) Exit"
PRINT Key$
PRINT "Poopy Toopy"
GOTO Menu1

selection: IF menu% = 1 THEN GOTO origin
IF menu% = 2 THEN GOTO writer
IF menu% = 3 THEN GOTO wipe
IF menu% = 4 THEN GOTO ref
IF menu% = 5 THEN END


thanks for any help you can give!
EclipseOTO

Anonymous

seems you're clearing your inkey buffer before keys can be read... try this:

Code:
Menu1:
do
Key$ = INKEY$
loop while key$ = ""
IF Key$ = CHR$(0) + CHR$(72) THEN LET menu% = menu% - 1
IF menu% = 0 THEN LET menu% = 1
IF Key$ = CHR$(0) + CHR$(80) THEN LET menu% = menu% + 1
IF menu% = 6 THEN LET menu% = 5
      
IF Key$ = CHR$(0) + CHR$(77) THEN GOTO selection

CLS
COLOR 4, 0, 0
PRINT "Spanish Helper Version 1.1"
COLOR 7, 0, 0
IF menu% = 1 THEN COLOR 7, 1, 0 ELSE COLOR 7, 0, 0
PRINT "1) Origin"
IF menu% = 2 THEN COLOR 7, 1, 0 ELSE COLOR 7, 0, 0
PRINT "2) Continue"
IF menu% = 3 THEN COLOR 7, 1, 0 ELSE COLOR 7, 0, 0
PRINT "3) Data wipe"
IF menu% = 4 THEN COLOR 7, 1, 0 ELSE COLOR 7, 0, 0
PRINT "4) Reference"
IF menu% = 5 THEN COLOR 7, 1, 0 ELSE COLOR 7, 0, 0
PRINT "5) Exit"
PRINT Key$
PRINT "Poopy Toopy"
GOTO Menu1

selection: IF menu% = 1 THEN GOTO origin
IF menu% = 2 THEN GOTO writer
IF menu% = 3 THEN GOTO wipe
IF menu% = 4 THEN GOTO ref
IF menu% = 5 THEN END
nice! It works great now! I'm pretty unclear on the whole inkey$ usage, can you explain why my code didn't work or give me a good link that explains inkey maybe?

Thanks,
EclipseOTO

EDIT: I forgot to add, is there a code for the "Enter" key? I can't seem to find it!

Anonymous

enter = chr$( 13 ), i believe...

the way inkey works is, when you hit a key, it is stored in the inkey buffer. when you call inkey, it spits out the keys that you pressed. FIFO (first in, first out).

in your program, the majority of the loop execution is spent below the while...wend. so that means that you're most likely to be in that point when you hit the keys. well, say you hit an arrow key, then, when you'd get to your while...wend, it would completely clean out the inkey buffer, until nothing was left, before it would allow execution to pass. then you would have nothing in your key$ variable, you see? =)
ah, okay that makes sense, thanks a lot!

EDIT: I've experimented a bit with the Inkey$ function and ran into the problem of in the beginning of my program, which has several sleep functions to delay the program, if enter is pressed multiple times I think it goes into the buffer multiple times. This wreaks havoc with the program and I was wondering if there was a way to clear the buffer without pausing the program!

Anonymous

sleep is a very unreliable mechanism for delaying a program (in qb. in fb you have SLEEP 2000, 1, which sleeps 2 seconds, and ignores all key input).

try something like this


Code:
dim timerDelay as double

timerDelay = TIMER + 2 '' I want to wait 2 seconds

do
loop until TIMER > timerDelay

for a reliable delay ^^
thanks for the more reliable timer, however this doesn't fix my problem. The problem is in the delayed sequence in which I'm using the timer if the enter key is pressed multiple times, when the program reaches the menu, it registers the enter as many times as it was pressed during the delayed sequence. Is there a way to clear this or change the program or something?
Quote:thanks for the more reliable timer, however this doesn't fix my problem. The problem is in the delayed sequence in which I'm using the timer if the enter key is pressed multiple times, when the program reaches the menu, it registers the enter as many times as it was pressed during the delayed sequence. Is there a way to clear this or change the program or something?

Use a loop like this to clear the keyboard buffer before displaying the menu.

do while not (inkey$="")
loop
I didn't know that one... So simple...
Is there a way I can use that sort of loop without requiring the user to press a key to continue with the program?
Pages: 1 2