KeyboardPushed not working

Mac OSX specific forum
dandersson
New User
New User
Posts: 8
Joined: Sat Nov 05, 2022 9:48 am

KeyboardPushed not working

Post 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)?
dandersson
New User
New User
Posts: 8
Joined: Sat Nov 05, 2022 9:48 am

Re: KeyboardPushed not working

Post 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.
User avatar
mk-soft
Always Here
Always Here
Posts: 6246
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: KeyboardPushed not working

Post 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
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Mijikai
Addict
Addict
Posts: 1520
Joined: Sun Sep 11, 2016 2:17 pm

Re: KeyboardPushed not working

Post by Mijikai »

Please close/end your programs properly.

Code: Select all

CloseScreen()
CloseWindow()
User avatar
mk-soft
Always Here
Always Here
Posts: 6246
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: KeyboardPushed not working

Post 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.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
dandersson
New User
New User
Posts: 8
Joined: Sat Nov 05, 2022 9:48 am

Re: KeyboardPushed not working

Post 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! :)
User avatar
mk-soft
Always Here
Always Here
Posts: 6246
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: KeyboardPushed not working

Post by mk-soft »

Read PB-Help description for OpenWindowedSrceen (complete) ;)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Post Reply