Event = WindowEvent()Description
Checks if an event has occurred on any of the opened windows.
The next event from the event queue and or zero if there are no more events. Unlike WaitWindowEvent() it doesn't wait for the next event - it always returns immediately. Event() can be used to get back this value.
This makes it useful for window event loops, where other processing needs to be done without waiting for an event to happen (e.g. Network transactions) and therefore WaitWindowEvent() can't be used.
It must be handled with care though if used on a continuing basis, because unlike WaitWindowEvent(), it will not give CPU time to other programs while waiting for an event and therefore consume all CPU power. In this case, either Delay() should be used somewhere in the loop or WaitWindowEvent() with a small timeout value.
To get the window number in which the event occurred, use the EventWindow() function.
Possible Events are :#PB_Event_Menu : a menu has been selected #PB_Event_Gadget : a gadget has been pushed #PB_Event_SysTray : an icon in the systray zone was clicked #PB_Event_Timer : a timer has reached its timeout #PB_Event_CloseWindow : the window close gadget has been pushed #PB_Event_Repaint : the window content has been destroyed and must be repainted (useful for 2D graphics operations) #PB_Event_SizeWindow : the window has been resized #PB_Event_MoveWindow : the window has been moved #PB_Event_MinimizeWindow : the window has been minimized #PB_Event_MaximizeWindow : the window has been maximized #PB_Event_RestoreWindow : the window has been restored to normal size (either from a minimum or maximum size) #PB_Event_ActivateWindow : the window has been activated (got the focus) #PB_Event_DeactivateWindow: the window has been deactivated (lost the focus) #PB_Event_WindowDrop : a Drag & Drop operation was finished on a window #PB_Event_GadgetDrop : a Drag & Drop operation was finished on a gadget #PB_Event_RightClick : a right mouse button click has occurred on the window. This can be useful to display a popup menu #PB_Event_LeftClick : a left mouse button click has occurred on the window #PB_Event_LeftDoubleClick : a left mouse button double-click has occurred on the windowA basic example for event handling can be found in the WaitWindowEvent() description.
After a #PB_Event_WindowDrop or #PB_Event_GadgetDrop Event, the event functions of the Drag & Drop library can be used to examine and read the dropped data.
Important: The window event loop should not be processed in a thread, as there is some limitation on OS X and Linux. A debugger error will be raised.
Correct way to handle an infinite WindowEvent() loop:Repeat Event = WindowEvent() If Event ; An event was in the queue so process it .... Else Delay(1) ; No event, let the others apps get some CPU time too ! EndIf Until Event = #PB_Event_CloseWindowImportant: The 'Delay' shouldn't be put after each event, because when lot of events will come (like refresh, gadgets updates etc..) the app will wait between each event. So the delay need to be put when no events are received. Other (recommened) ways is to use WaitWindowEvent() with a small timeout value, or setup a timer with AddWindowTimer().
Important: If you use a FlipBuffers() somewhere in your code, so the 'Delay(1)' is not necessary.
Example: n°1 With Gadget
If OpenWindow(0, 0, 0, 600, 100, "Position of the mouse on the window: ", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) TextGadget(0, 10, 6, 200, 20, "") Repeat Event = WindowEvent() If Event <> 0 ; All events are treated, so we can display the coordinates of the mouse SetWindowTitle(0, "Position of the mouse on the window: " + Str(WindowMouseX(0)) + "," + Str(WindowMouseX(0))) Else Delay(1) ; Without a FlipBuffers(), Delay() frees the CPU for the multitasking EndIf Until Event = #PB_Event_CloseWindow EndIf
Example: n°2 With Gadget
If OpenWindow(0, 0, 0, 300, 30, "Position of the mouse on the desktop", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) TextGadget(0, 10, 6, 200, 20, "") Repeat Event = WindowEvent() If Event = 0 ; No more events in the queue, so we can display the coordinates of the mouse SetGadgetText(0, "Coordinates: "+Str(DesktopMouseX())+","+Str(DesktopMouseY())) EndIf Delay(20) ; Without a FlipBuffers(), Delay() frees the CPU for the multitasking Until Event = #PB_Event_CloseWindow EndIf
Example: Without Gadgets (General case)
;Few variables BallX = 400 BallY = 200 BallSpeedY.f = 5 Gravitation.f = 2 ;Initialization If InitSprite() InitKeyboard() InitMouse() EndIf ;Window OpenWindow(0, 0, 0, 800, 600, "WindowEvent", #PB_Window_SystemMenu|#PB_Window_ScreenCentered) OpenWindowedScreen(WindowID(0), 0, 0, 800, 600) ;Ground Ground = CreateSprite(#PB_Any, 800, 30) StartDrawing(SpriteOutput(Ground)) Box(0,0,800,30,RGB(128, 0, 0)) StopDrawing() ;Ball Ball = CreateSprite(#PB_Any, 16, 16) StartDrawing(SpriteOutput(Ball)) Box(0,0,16,16,RGB(135, 206, 235)) Circle(8,8,8,RGB(255, 255, 0)) StopDrawing() ;Gauge Image = CreateImage(#PB_Any, 8, 8, 24, RGB(255, 255, 255)) *Buffer=EncodeImage(Image ,#PB_ImagePlugin_BMP) Gauge = CatchSprite(#PB_Any, *Buffer) ;Main Loop Repeat Repeat ;Window events ;==================================== ;Try all possibilities but only one at a time Event = WindowEvent() ; Animation ;Event = WaitWindowEvent() ; Blocking animation ;Event = WaitWindowEvent(1) ; Animation but a 1ms unnecessary delay and furthermore it's a ; bad way to program events because the queue is not empty Select Event Case #PB_Event_CloseWindow End EndSelect Until Event=0 FlipBuffers() ; ==> With WindowEvent(), FlipBuffers() frees the CPU for the multitasking so Delay(1) is not necessary ClearScreen(RGB(135, 206, 235)) ExamineKeyboard() ;Keyboard DisplaySprite(Gauge, 50, 570-BallY) ;Display the gauge ZoomSprite(Gauge, 20, 570) DisplaySprite(Ground, 0, 570) ;Display the ground DisplaySprite(Ball, BallX, BallY) ;Display the ball ;Ball Movement BallSpeedY = BallSpeedY + Gravitation BallY = BallY + BallSpeedY ;Management of the collision of the ball with the ground If SpriteCollision(Ball, BallX, BallY+16, Ground, 0, 570) BallY= 554 BallSpeedY = -BallSpeedY EndIf Until KeyboardPushed(#PB_Key_Escape)
WaitWindowEvent(), Event(), EventWindow(), EventGadget(), EventMenu(), EventTimer(), EventData(), EventType(), PostEvent(), BindEvent(), UnbindEvent()