Page 1 of 1

Key Press

Posted: Tue Aug 23, 2011 11:46 pm
by Nubcake
Hello

I have tried to create my own function for key presses where it will detect only once if a key has been pressed and released. All seems fine with it however when i try to get it to work with more than one key it fails. It will only work if the same key that was last pressed is pressed again before another key can used. I'm still a newbie with PB so forgive my mistakes :D

Code: Select all

Global press=1
Procedure KeyboardPressed(KeyID)
  If press=1 
    If KeyboardPushed(KeyID)
      press=0
      ProcedureReturn 1
    EndIf
  ElseIf press=0 And KeyboardReleased(KeyID)
    press=1
  EndIf
EndProcedure

If Camera\move=0
  If Inventory\pause=0
    If KeyboardPressed(#PB_Key_Return)
     Inventory\pause=1
    EndIf
  ElseIf Inventory\pause=1
    If KeyboardPressed(#PB_Key_Return)
      Inventory\pause=0
    EndIf
  EndIf
  If Inventory\pause=1
    DisplaySprite(#Inventory,0,16)
    DisplayTransparentSprite(#Selector,Inventory\x,Inventory\y)
    If KeyboardPressed(#PB_Key_Right)
      Inventory\x+32
    EndIf
  EndIf
 EndIf
What happens is when i press enter it pauses which is should do but when i press right i can only do it once before i have to press enter again to 'release' the key so i can press right again. Is there some way around this? Throughout searching i only saw that people use key releases to execute actions but not key presses. ExamineKeyboard() is in the game loop above the key pressing, if needed i can post more code.

Re: Key Press

Posted: Wed Aug 24, 2011 5:16 am
by kenmo
It's always tricky to find problems without a runnable piece of code... BUT your issue is probably caused by using a single "press" variable for all key checks... because of this, the calls to KeyboardPressed() are dependent on the previous one, and dependent on the order you write them in the code!

I usually spare a little extra memory and save all the current and previous keystates of my desired keys each frame. This makes keyboard event handling dead simple.

Here's an example:

Code: Select all

InitSprite()
InitKeyboard()
OpenWindow(0, 0, 0, 320, 240, "Keyboard Demo: Press Up/Down", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, WindowWidth(0), WindowHeight(0), 0, 0, 0)

; ***** You can customize the number of keys, and their codes here
#Keys = 2
Dim KeyCode.i(#Keys - 1)
  KeyCode(0) = #PB_Key_Up
  KeyCode(1) = #PB_Key_Down
Dim KeyState.i(#Keys - 1, 1)

ThisFrame.i = 0
LastFrame.i = 1

Repeat
  Event = WaitWindowEvent(25)
  
  ; ***** Update key states (you can put this into a tidy procedure)
  LastFrame = ThisFrame
  ThisFrame = 1 - LastFrame
  ExamineKeyboard()
  For i.i = 0 To #Keys - 1
    KeyState(i,ThisFrame) = KeyboardPushed(KeyCode(i))
  Next i
  
  ; Draw display
  ClearScreen($000000)
  If (StartDrawing(ScreenOutput()))
    
    For i.i = 0 To #Keys - 1
    
      ; ***** (You can put these checks into tidy macros like KeyboardHit(code).)
    
      ; Check if the key has just now been hit
      If (KeyState(i,ThisFrame) And (Not KeyState(i,LastFrame)))
        Circle(WindowWidth(0)/4, (i+1)*WindowHeight(0)/3, 20, #Green)
      EndIf
    
      ; Check if the key is down
      If (KeyState(i,ThisFrame))
        Circle(WindowWidth(0)/2, (i+1)*WindowHeight(0)/3, 20, #Yellow)
      EndIf
    
      ; Check if the key has just now been released
      If ((Not KeyState(i,ThisFrame)) And KeyState(i,LastFrame))
        Circle(3*WindowWidth(0)/4, (i+1)*WindowHeight(0)/3, 20, #Red)
      EndIf
      
    Next i
    
    StopDrawing()
    FlipBuffers()
  EndIf
  
Until Event = #PB_Event_CloseWindow
End
The keyboard events are all independent, and you detect press, hold, and release on each. I've marked the important parts with *****.

Re: Key Press

Posted: Wed Aug 24, 2011 12:28 pm
by Nubcake
Thanks for the help :D I was pretty sure that the problem was that it was something to do with KeyboardReleased() , anyway i managed to detect what keys were being pressed by using Select\EndSelect:

Code: Select all

 Select KeyCode(i)
          Case #PB_Key_Up
            Debug "Up Pressed"
          Case #PB_Key_Down
            Debug "Down Pressed"
EndSelect