brl_eventqueue.md 8.0 KB


id: brl.eventqueue title: BRL.EventQueue

sidebar_label: BRL.EventQueue

The event queue is a simple first-in, first-out queue that is used to collect TEvent objects emitted by your application.

The PollEvent and WaitEvent commands can be used to receive the next event from the event queue, while the PeekEvent command can be used to check if the event queue is empty.

Events are added to the event queue using PostEvent.

Functions

Function PeekEvent:TEvent()

Examine the next event in the event queue

PeekEvent examines the next event in the event queue, without removing it from the event queue or modifying the CurrentEvent global variable.

If there are no events in the event queue, PeekEvent returns Null.


Function PollEvent:Int()

Get the next event from the event queue

PollEvent removes an event from the event queue and updates the CurrentEvent global variable.

If there are no events in the event queue, PollEvent returns 0.

Returns

The id of the next event in the event queue, or 0 if the event queue is empty


Function WaitEvent:Int()

Get the next event from the event queue, waiting if necessary

WaitEvent removes an event from the event queue and updates the CurrentEvent global variable.

If there are no events in the event queue, WaitEvent halts program execution until an event is available.

Returns

The id of the next event in the event queue


Function PostEvent( event:TEvent,update:Int=False )

Post an event to the event queue

PostEvent adds an event to the end of the event queue.

The update flag can be used to update an existing event. If update is True and an event with the same id and source is found in the event queue, the existing event will be updated instead of event being added to the event queue. This can be useful to prevent high frequency events such as timer events from flooding the event queue.


Function EventID:Int()

Get current event id

Returns

The id field of the CurrentEvent global variable

Example 1

SuperStrict

Import MaxGUI.Drivers

Local MyWindow:TGadget=CreateWindow("Button Example", 200,200,320,240)
Local MyButton:TGadget=CreateButton("Click Me",140,60,80,40, MyWindow)

Repeat
  WaitEvent()
  Select EventID()
  Case EVENT_WINDOWCLOSE
     End
   End Select
Forever

Example 2

SuperStrict

Import MaxGUI.Drivers

Local window:tgadget=CreateWindow("events",0,0,320,240)

Local button:tgadget=CreateButton("Button1",4,4,80,24,window)
Local canvas:tgadget=CreateCanvas(84,4,80,24,window,1)

Repeat
	WaitEvent()
	If EventID()=EVENT_WINDOWCLOSE End
	
	Select EventID()
		Case EVENT_GADGETACTION Print "gadgetaction (buttonpress, etc.)"
		Case EVENT_MOUSEMOVE Print "canvas mousemove"
	End Select
	
Forever


Function EventData:Int()

Get current event data

Returns

The data field of the CurrentEvent global variable

Example

SuperStrict

Import MaxGUI.Drivers

Local window:tgadget=CreateWindow("leftclick/rightclick",0,0,320,240)

Local canvas:tgadget=CreateCanvas(4,4,160,160,window,1)

Repeat
	WaitEvent()
	If EventID()=EVENT_WINDOWCLOSE End
	
	Select EventData()
		Case 1 Print "leftclick"
		Case 2 Print "rightclick"
	End Select
Forever


Function EventMods:Int()

Get current event modifiers

Returns

The mods field of the CurrentEvent global variable


Function EventX:Int()

Get current event x value

Returns

The x field of the CurrentEvent global variable

Example

SuperStrict

Import MaxGUI.Drivers

Local window:tgadget=CreateWindow("move mouse",0,0,320,240)

Local canvas:tgadget=CreateCanvas(4,4,160,160,window,1)

Repeat
	WaitEvent()
	If EventID()=EVENT_WINDOWCLOSE End
	
	If EventID()=EVENT_MOUSEMOVE
		Print EventX()
	EndIf
Forever


Function EventY:Int()

Get current event y value

Returns

The y field of the CurrentEvent global variable

Example

SuperStrict

Import MaxGUI.Drivers

Local window:tgadget=CreateWindow("move mouse",0,0,320,240)

Local canvas:tgadget=CreateCanvas(4,4,160,160,window,1)

Repeat
	WaitEvent()
	If EventID()=EVENT_WINDOWCLOSE End
	
	If EventID()=EVENT_MOUSEMOVE
		Print EventY()
	EndIf
Forever


Function EventExtra:Object()

Get current event extra value

Returns

The extra field of the CurrentEvent global variable


Function EventText$()

Get current event extra value converted to a string

Returns

The extra field of the CurrentEvent global variable converted to a string

Example

'
' Drop a file onto the window to see EventText() return the path.
'
SuperStrict

Import MaxGUI.Drivers

Local window:tgadget=CreateWindow("move mouse",0,0,320,240,Null,15|WINDOW_ACCEPTFILES)

Repeat
	WaitEvent()
	If EventID()=EVENT_WINDOWCLOSE End
	
	If EventID()=EVENT_WINDOWACCEPT
		Print EventText()
	EndIf
Forever


Function EventSource:Object()

Get current event source object

Returns

The source field of the CurrentEvent global variable

Example 1

SuperStrict

Import MaxGUI.Drivers

Local MyWindow:TGadget=CreateWindow("Two Buttons Example", 200,200,320,240)
Local Button1:TGadget=CreateButton("One",140,40,80,40, MyWindow)
Local Button2:TGadget=CreateButton("Two",140,100,80,40, MyWindow)

Repeat
  WaitEvent()
  Select EventID()
  Case EVENT_WINDOWCLOSE
     End
  Case EVENT_GADGETACTION
    Select EventSource()
      Case Button1
         SetGadgetText(Button1,"One clicked")
      Case Button2
         SetGadgetText(Button2,"Two clicked")
      End Select
   End Select
Forever

Example 2

SuperStrict

Import MaxGUI.Drivers

Local window:tgadget=CreateWindow("events",0,0,320,240)

Local button1:tgadget=CreateButton("Button1",4,4,80,24,window)
Local button2:tgadget=CreateButton("Button2",84,4,80,24,window)

Repeat
	WaitEvent()
	If EventID()=EVENT_WINDOWCLOSE End
	
	Select EventSource()
		Case button1 Print "button1"
		Case button2 Print "button2"
	End Select
	
Forever


Function EventSourceHandle:Size_T()

Get current event source object handle

Returns

The source field of the CurrentEvent global variable converted to an integer handle


Globals

Global CurrentEvent:TEvent=NullEvent

Current Event

The CurrentEvent global variable contains the event most recently returned by PollEvent or WaitEvent.