Main Menu

keyboard input

Started by dangerousbrian, July 27, 2005, 03:10:20 PM

Previous topic - Next topic

dangerousbrian

I'm trying to include a choice in a sub routine called royal: I want the user to be able to either repeat the subroutine(thats the reap: label) or return to the main programme. But the progamme just sails through the inkey$ & returns to the main programme. Can anyone tell me why this isn't working? :(

royal:
reap:
;about 50 lines of code fit in here
c$=""
test:
c$=Inkey$():Wait 1000
If c$="" Then Goto test
If c$="y" Then Goto reap
Return

Draco9898

not sure what you want, try this


Do
Cls 0
If MyFlag>0
 Text 10,10,"subroutine here"
 `50 lines of code here
Else
 Text 10,10,"Repeat subroutine?"
 Text 10,30,"press A if you want to"
 If KeyState(30)=1 Then MyFlag=50
EndIf
Sync
Loop
DualCore Intel Core 2 processor @ 2.3 ghz, Geforce 8600 GT (latest forceware drivers), 2 gigs of ram, WIN XP home edition sp2, FireFox 2.

"You'll no doubt be horrified to discover that PlayBasic is a Programming Language." -Kevin

kevin

Brian,

The code looks like it would work to me.  What's prolly happening is it's not waiting for the key to be released.. So if you called that code in  loop, pressing the "y" key would most likely make it drop through not just this loop but some future loops.




Also, You might want to look into a REPEAT/UNTIL loops

Ie

print "Press Y or N to continue"
Sync

; Start of Repeat/Until loop
Repeat
  ; read the string Character that is currently being pressed (if any)
   KeyPressed$=inkey$()
  ; Force this character to be lower case
   KeyPressed$=Lower$(keypressed$)
  ; Check if the character is either the Y or N characters.
  ; if it's NOT, then it'll jump back to the REPEAT statement and continue
  ; looping
Until KeyPressed$="y" or KeyPressed$="n"

;wait until NO keys are being pressed
waitnokey

; Print the LeyPRessed variable
print KeyPressed$
print "done"
Sync
waitkey




This code will repeat(loop) until either the Y or N keys have been pressed.

it uses the WaitNoKey command to stop it from falling through the following "waitkey" command. Since it's likely the routnie will run faster than the user can press and remove their finger from the key.

dangerousbrian

#3
Thanks Draco & Kevin. I tried all sorts of variations with inkey$. Most worked on the first runthrough, but despite having turned keypress$ into a "" (I believe thats a null $) on subsequent runthroughts it still remembered  the original input.

However the below works perfectly. Placed at the end of the main programme I can repeat the sub routine as often as I like or go back to the start

theend:
Repeat
WaitKey
If LeftKey()=1 Then Gosub royal:Goto theend
If RightKey()=1 Then Goto start
Until LeftKey()=1 Or RightKey()=1