WaitWindowEvent()

Syntax

Event = WaitWindowEvent([Timeout])
Description
Wait until an event occurs. It's the same function as WindowEvent() but locks the program execution, which is very important in a multitasking environment.

Parameters

Timeout (optional) The timeout (in milliseconds) which causes the function to return if no events are occurring. If no timeout is specified, it will wait infinitely until an event occurs.

Return value

Return the event which occurred, see WindowEvent() for more information. Event() can be used to get back this value.

Remarks

An application should always use this function instead of WindowEvent() if possible, as it doesn't takes an CPU time while waiting for an event.

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.

WaitWindowEvent() can only be called once per event loop, because else events will be "lost" (every event can only be processed once and isn't available anymore for a second time after first processing).

Example: General case

  If OpenWindow(0, 0, 0, 230, 90, "Event handling example...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

   ButtonGadget  (1, 10, 10, 200, 20, "Click me")
   CheckBoxGadget(2, 10, 40, 200, 20, "Check me")

   If CreateMenu(0, WindowID(0))
     MenuTitle("Menu")
     MenuItem(1, "Item 1")
     MenuItem(2, "Item 2")
     MenuItem(3, "Item 3")
   EndIf

   Repeat
     Event = WaitWindowEvent()
     
     Select Event
     
       Case #PB_Event_Gadget
         Select EventGadget()
           Case 1 : Debug "Button 1 clicked!"
           Case 2 : Debug "Button 2 clicked!"
         EndSelect
       
       Case #PB_Event_Menu
         Select EventMenu()
           Case 1 : Debug "Menu item 1 clicked!"
           Case 2 : Debug "Menu item 2 clicked!"
           Case 3 : Debug "Menu item 3 clicked!"
         EndSelect
     
     EndSelect
   Until Event = #PB_Event_CloseWindow
 EndIf

Example: With a TimeOut

  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 = WaitWindowEvent(20)
      
      If Event = 0 ; No more events in the queue, so let's display the mouse coordinates
        SetGadgetText(0, "Coordinates: " + Str(DesktopMouseX()) + "," + Str(DesktopMouseY()))  
      EndIf
       
    Until Event = #PB_Event_CloseWindow
  EndIf

Example: With a Timer

  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, "")
    AddWindowTimer(0, 0, 10) ; Timeout = 10 ms
    
    Repeat
      Event = WaitWindowEvent()
      If Event = #PB_Event_Timer ; Each 10 ms => Let's display the coordinates
      SetGadgetText(0, "Coordinates: " + Str(DesktopMouseX()) + "," + Str(DesktopMouseY()))  
      EndIf
       
    Until Event = #PB_Event_CloseWindow
  EndIf

See Also

WindowEvent(), Event(), EventWindow(), EventGadget(), EventMenu(), EventTimer(), EventData(), EventType(), PostEvent(), BindEvent(), UnbindEvent()

Supported OS

All

<- UnbindEvent() - Window Index - WindowBounds() ->