Toggle on KeyboardPushed?
Posted: Fri May 11, 2012 11:45 am
Is there some neater code to toggle the flag var on KeyboardPushed() inside the screen rendering loop? The problem is we have to check if the key was released before it was pushed for the next time (similarly as KeyboardReleased() checks if the key was first pushed). Here's my code to toggle the onscreen info with the F9:
There must be a way to do this part:
in a more clever way :-/ It's too much compared to:
I know the KeyboardReleased() does the most but anyway...
Code: Select all
#screen_width = 960
#screen_height = 720
#window_0 = $00
#title = "Toggle on KeyboardPushed()"
Global done.b = $00 ;flag
Global debug_info.b = $ff ;flag
If InitSprite() = 0 Or InitKeyboard() = 0 Or InitSound() = 0
MessageRequester("Error","InitSprite or InitKeyboard or InitSound",0)
End
EndIf
If OpenWindow(#window_0, 0, 0, #screen_width, #screen_height, #Title, #PB_Window_SystemMenu | #PB_Window_TitleBar | #PB_Window_ScreenCentered) = 0
MessageRequester("Error","OpenWindow",0)
End
EndIf
If OpenWindowedScreen(WindowID(0),0,0,#screen_width, #screen_height,0,0,0) = 0
MessageRequester("Error","OpenWindowedScreen",0)
End
EndIf
Procedure process_input()
Static info_pushed.b = $00
Event = WindowEvent()
If Event = #PB_Event_CloseWindow
done = 1 ;remember 'close' event
EndIf
ExamineKeyboard()
If KeyboardPushed(#PB_Key_Escape)
done = 1 ;remember ESC key
EndIf
;if F9 was pushed, check for 'released'
If info_pushed And Not KeyboardPushed(#PB_Key_F9)
info_pushed = $00 ;remember 'F9 released'
EndIf
;if F9 was released, check for 'pushed'
If Not info_pushed And KeyboardPushed(#PB_Key_F9)
debug_info ! $ff ;toggle the flag
info_pushed = $ff ;remember 'F9 pushed'
EndIf
EndProcedure
SetFrameRate(60)
Repeat
ClearScreen($000000)
process_input()
If debug_info
If StartDrawing(ScreenOutput())
DrawText(5, 5, "Some info... (press F9 to toggle)", $0000ff)
StopDrawing()
EndIf
EndIf
;screen rendering
FlipBuffers()
Until done = 1
End
Code: Select all
If info_pushed And Not KeyboardPushed(#PB_Key_F9)
info_pushed = $00
EndIf
If Not info_pushed And KeyboardPushed(#PB_Key_F9)
debug_info ! $ff
info_pushed = $ff
EndIf
Code: Select all
If KeyboardReleased(#PB_Key_F9)
debug_info ! $ff
EndIf