Page 1 of 1

KeyboardPushed not working

Posted: Sat Nov 05, 2022 9:52 am
by dandersson
Just starting out with PureBasic and my first attempt is a simple space invaders-clone. I'd like to be able to exit with escape, but it doesn't seem to work as advertised.

My code:

Code: Select all

InitSprite()
OpenWindow(0, 300, 300, 800, 600, "Spel", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600);
InitKeyboard()

x = 0
Repeat
	x = x + 1
	
	FlipBuffers()
	
	If StartDrawing(ScreenOutput())
		DrawText(x, 0, "Press ESC to quit")
		
		StopDrawing()
	EndIf
	
	ExamineKeyboard()
Until (x = 100) Or (KeyboardPushed(#PB_Key_Escape) > 0)
End
Am I misunderstanding or isn't PureBasic up to scratch when it comes to M1 Macs (MacOS 12.6)?

Re: KeyboardPushed not working

Posted: Sat Nov 05, 2022 11:24 am
by dandersson
I added

Code: Select all

Debug(KeyboardReleased(#PB_Key_All))
and during the whole loop, it always returns 0 no matter which key I press.

Re: KeyboardPushed not working

Posted: Sat Nov 05, 2022 12:50 pm
by mk-soft
No bug! But it is often forgotten.

When using OpenWindowedSrceen, the Windows events must always be queried as well.

Code: Select all

;-TOP
If Not InitSprite()
  MessageRequester("Error", "Can't open screen & sprite environment!", 0)
  End
EndIf

If Not InitKeyboard()
  MessageRequester("Error", "Init keyboard faild!")
  End
EndIf

If OpenWindow(0, 300, 300, 800, 600, "Spel", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

  If OpenWindowedScreen(WindowID(0), 0, 0, 800, 600);
    
    x = 0
    Paused = #False
    Repeat
      ; Dispatch all window events
      While WindowEvent() : Wend
      
      FlipBuffers() 
      ClearScreen(RGB(0, 0, 0))
      
      If StartDrawing(ScreenOutput())
        
        ExamineKeyboard()
        If KeyboardReleased(#PB_Key_P)
          If Paused = #False
            Paused = #True
          Else
            Paused = #False
          EndIf
        EndIf
        
        DrawingMode(0)
        
        If Paused = #False
          DrawText(20, 20, "Program is running...")
        Else
          DrawText(20, 20, "Program paused...    ")
        EndIf
        
        StopDrawing()
      EndIf
      
    Until KeyboardPushed(#PB_Key_Escape)	
    
  EndIf
EndIf

Re: KeyboardPushed not working

Posted: Sat Nov 05, 2022 1:21 pm
by Mijikai
Please close/end your programs properly.

Code: Select all

CloseScreen()
CloseWindow()

Re: KeyboardPushed not working

Posted: Sat Nov 05, 2022 1:39 pm
by mk-soft
Mijikai wrote: Sat Nov 05, 2022 1:21 pm Please close/end your programs properly.

Code: Select all

CloseScreen()
CloseWindow()
Not required when exiting the programme. Purebasic cleans up automatically.

Exception:
If you work with threads under macOS, the threads must be terminated by the program. Running threads when exiting the porgam are not liked by macOS.

Re: KeyboardPushed not working

Posted: Mon Nov 07, 2022 9:13 am
by dandersson
mk-soft wrote: Sat Nov 05, 2022 12:50 pm No bug! But it is often forgotten.

When using OpenWindowedSrceen, the Windows events must always be queried as well.
Thank you!

So the "only" thing I was missing was:

Code: Select all

While WindowEvent() : Wend
correct?

Wish the documentation would have mentioned it! :)

Re: KeyboardPushed not working

Posted: Mon Nov 07, 2022 9:43 am
by mk-soft
Read PB-Help description for OpenWindowedSrceen (complete) ;)