Events

All events implement the Msg interface. The framework sends them to your Update method.

Keyboard

go
type KeyMsg struct {
    Type KeyType
    Rune rune   // only valid when Type == KeyRune
    Alt  bool
}

Key Types

ConstantDescription
KeyRuneRegular character (check Rune field)
KeyEnterEnter/Return
KeyTab / KeyBacktabTab / Shift+Tab
KeyBackspace / KeyDeleteBackspace / Delete
KeyEscapeEscape
KeySpaceSpace
KeyUp / KeyDown / KeyLeft / KeyRightArrow keys
KeyHome / KeyEndHome / End
KeyPageUp / KeyPageDownPage Up / Page Down
KeyInsertInsert
KeyF1 through KeyF12Function keys
KeyCtrlA through KeyCtrlZCtrl+letter combinations

Pattern Matching

go
switch msg := msg.(type) {
case tui.KeyMsg:
    switch msg.Type {
    case tui.KeyCtrlC:
        return a, tui.QuitCmd()
    case tui.KeyRune:
        switch msg.Rune {
        case 'q':
            return a, tui.QuitCmd()
        }
    }
}

Mouse

go
type MouseMsg struct {
    X, Y   int
    Button MouseButton
    Action MouseAction
    Alt, Ctrl, Shift bool
}
ButtonDescription
MouseLeft / MouseMiddle / MouseRightStandard buttons
MouseWheelUp / MouseWheelDownScroll wheel
MouseNoneNo button (motion event)
ActionDescription
MousePressButton pressed
MouseReleaseButton released
MouseMotionMouse moved

Resize

go
type ResizeMsg struct {
    Width, Height int
}

Sent when the terminal dimensions change.

Lifecycle

MessageDescription
QuitMsgApplication should exit
FocusMsgTerminal gained focus
BlurMsgTerminal lost focus
SuspendMsgApp is suspending (Ctrl+Z)
ResumeMsgApp resumed from suspension

Timer

MessageDescription
TickMsgPeriodic tick (from TickCmd)
AnimationTickMsgAnimation tick (from AnimateCmd)

Focus

go
type FocusChangeMsg struct {
    Previous string
    Current  string
}

Cursor

go
type CursorMsg struct {
    X, Y    int
    Visible bool
}

Timer Commands

FunctionDescription
TickCmd(duration)Send TickMsg after duration
TickEvery(fps)Send TickMsg at FPS rate
AfterCmd(duration, msg)Send custom msg after delay
PeriodicCmd(interval, fn)Call fn at interval
AnimateCmd(fps)Send AnimationTickMsg at FPS