KeyDown problem

Blitz3D Forums/Blitz3D Programming/KeyDown problem

John Blackledge(Posted 2005) [#1]
I've always had a problem with KeyDown:

Suppose I want to exit from the 3D scene back to the main menu by pressing the Escape key.
Once I'm on the main menu then pressing Escape would trigger the 'Exit' question.

The trouble is that any function I write always 'drops through' no matter how many times I use FlushKeys.

Here's a simple example:
[CODE]
Repeat
FlushKeys
If KeyDown(57)
Print "Key Down"
While KeyDown(57)
Wend
Print "Key Up"
EndIf
Until KeyDown(1)
End
[/CODE]
The first time the spacebar is pressed Blitz just drops through the While KeyDown(57) loop.
While KeyDown(whatever) just can't be trusted.
Does anyone have a tried and tested method of 'holding until no key is pressed'?


Rook Zimbabwe(Posted 2005) [#2]
I don't see that it doesn't work like you wanted... I plonked it in a simple framework and it works... try this:
Graphics3D 800,600,32,1
SetBuffer BackBuffer()

Repeat 
FlushKeys 
If KeyDown(57) 
Print "Key Down" 
While KeyDown(57) 
Wend 
Print "Key Up" 
EndIf 
Until KeyDown(1) 
End


  UpdateWorld()
  RenderWorld()
  
  Flip

End


OH FYI use lower case letters {code} not {CODE} :)
-RZ


Vorderman(Posted 2005) [#3]
or use keyhit instead of keydown to grab a single keypress only.


DJWoodgate(Posted 2005) [#4]
Well spotted Rook, it seems to be one of those things that is only correctly initialised when setting a graphics mode for some odd reason, probably a holdover from old code.


jfk EO-11110(Posted 2005) [#5]
You need to use Flushkeys when you want to use KeyHit or Inkey. If you decide to use KeyDown, you can do something like this:

; submenu
while not keydown(1)
; gfx etc
wend

while keydown(1) ; wait until the user releases the key
 delay 1
wend

;mainmenu
while not keydown(1)
; gfx etc.
wend


of course, it may be a better idea to use a function, somethin like
function waitNoaction()
 action=0
 repeat
  for i=0 to 255
   if keydown(i)=1 then 
    action=1
    exit
   endif
  next
  if mousedown(1)then action=1
  if mousedown(2)then action=1
 until action=0
end function


But like the first example this is a OnMouseUp thing. If you want the exit condition on MouseDown, you need to use a flag variable, something like:

; submenu
while not keydown(1)
 ;..
wend

key1=1

; menu2
while (keydown(1)=0) or (key1=1)
 if keydown(1)=0 then key1=0
 ;...
wend



Rook Zimbabwe(Posted 2005) [#6]
@DJW
Thanks! I bet you are right about it being a holdover...

@jfk
Wow thats a lot of different code!

-RZ


John Blackledge(Posted 2005) [#7]
Thanks, Rook.
I would never have thought that Graphics3D could have that effect.
Now that I have a well-behaved piece of code I can look back at the full misbehaving code.
And thanks for mentioning [code] lower case.


Rook Zimbabwe(Posted 2005) [#8]
Hey we are here to help with misbehaving code... just ask! :)
-RZ