Skip to content

Events ​

Register a Go function to get a handler ID, then reference that ID from an element. Handlers run in order on the goroutine that calls DispatchEvents, never during painting.

RegistrationUse it with
app.OnClick(fn)Node.OnClick
app.OnEvent(fn)Node.Interact, BindKey, OnAction
app.OnTextChange(fn)TextInputOptions.OnChange
app.RemoveHandler(id)Removes a handler. Events already queued for it become harmless

Each function has the signature func(context.Context, *client.Event) error. A handler returning an error stops DispatchEvents, which returns that error. Handler IDs are never reused.

Event fields ​

Every *client.Event carries:

FieldDescription
SequencePosition in the event stream
Window, ViewHandles of the source window and view
RevisionThe view revision the event was captured from
ElementIdThe element that reported the event
HandlerIdThe handler it was dispatched to

Payloads by kind ​

Read the payload with the getter matching the kind. Getters for other kinds return nil, so one handler can serve several event kinds.

GetterReported byFields
GetClick()OnClickPosition, Button, Modifiers, ClickCount
GetKeyDown()Interaction.KeyDownKey, Modifiers, Held
GetKeyUp()Interaction.KeyUpKey, Modifiers, Held
GetMouseDown()Interaction.MouseDownPosition, Button, Modifiers
GetMouseUp()Interaction.MouseUpPosition, Button, Modifiers
GetMouseMove()Interaction.MouseMovePosition, Button, Modifiers, Dragging
GetScroll()Interaction.ScrollPosition, Modifiers, Delta in pixels
GetFocusChanged()Interaction.FocusChangedFocused
GetFileDrop()Interaction.FileDropPaths
GetDataDrop()Interaction.DataDropKind, Payload, Preview
GetAction()Key bindings, OnActionName, Payload
GetTextChanged()TextInputOptions.OnChangeValue, Epoch, EditRevision, Composing

Modifiers has the booleans Control, Alt, Shift and Platform, where Platform is the Command key on macOS. Button is client.MouseLeft, client.MouseRight or client.MouseMiddle.

A list of three received action events with their element, name and payload

One handler receiving three actions sent with Window.DispatchAction, reading ElementId and GetAction().

Handling several kinds ​

go
inspect := app.OnEvent(func(ctx context.Context, event *client.Event) error {
	switch kind := event.Kind.(type) {
	case *client.EventMouseDown:
		fmt.Println("down at", kind.MouseDown.Position.X, kind.MouseDown.Position.Y)
	case *client.EventScroll:
		fmt.Println("scrolled", kind.Scroll.Delta.Y)
	}
	return nil
})

Delivery ​

Events are held until the next NextEvents call acknowledges them, so canceling a pending pull can be retried without losing any. Acknowledgement means the batch was received, not that every handler succeeded.

Released under the Apache License 2.0. The optional Zed editor component is GPL-3.0-or-later.