Skip to content

core.ui.event

This module provides event types and classes for the terminal UI system.

TIP

To use this module, you need to import it first: import("core.ui.event")

NOTE

The UI module is primarily used for xmake's internal xmake f --menu menu-based visual configuration. It provides basic UI components that can also be used by users to implement their own terminal UIs.

The event module provides a unified way to handle different types of events in the terminal UI, including keyboard events, mouse events, command events, text events, and idle events.

Event Types

The module defines the following event types:

Event TypeValueDescription
event.ev_keyboard1Keyboard key press/release event
event.ev_mouse2Mouse button/movement event
event.ev_command3Command event
event.ev_text4Text input event
event.ev_idle5Idle event (no user input)
event.ev_max5Maximum event type value

Command Event Types

The module defines the following command event types:

Command TypeValueDescription
event.cm_quit1Quit application
event.cm_exit2Exit current view
event.cm_enter3Enter/confirm action
event.cm_max3Maximum command type value

event:is_key

  • Check if event is a keyboard event with specific key

Function Prototype

API

lua
event:is_key(key_name: <string>)

Parameter Description

ParameterDescription
key_nameRequired. Name of the key to check (e.g., "Enter", "Esc", "Tab")

Return Value

TypeDescription
booleanReturns true if event is keyboard event for specified key, false otherwise

Usage

Check if the event is a specific keyboard event:

lua
import("core.ui.event")

function view:on_event(e)
    if e:is_key("Enter") then
        print("Enter key pressed")
        return true  -- Event handled
    elseif e:is_key("Esc") then
        self:quit()
        return true
    end
    return false
end

event:is_command

  • Check if event is a command event with specific command

Function Prototype

API

lua
event:is_command(command: <string>)

Parameter Description

ParameterDescription
commandRequired. Command name to check (e.g., "cm_quit", "cm_enter")

Return Value

TypeDescription
booleanReturns true if event is command event for specified command, false otherwise

Usage

Check if the event is a specific command event:

lua
import("core.ui.event")

function view:on_event(e)
    if e:is_command("cm_quit") then
        self:quit()
        return true
    elseif e:is_command("cm_enter") then
        self:on_confirm()
        return true
    end
    return false
end

event:dump

  • Dump event information for debugging

Function Prototype

API

lua
event:dump()

Parameter Description

No parameters

Return Value

No return value

Usage

Print event information for debugging:

lua
import("core.ui.event")

function view:on_event(e)
    e:dump()  -- Output: event(key): Enter 10 .. or event(cmd): cm_quit ..
end

Event Objects

event.keyboard

  • Create a keyboard event object

Function Prototype

API

lua
event.keyboard(key_code: <number>, key_name: <string>, key_meta: <boolean>)

Parameter Description

ParameterDescription
key_codeRequired. Numeric key code
key_nameRequired. Key name string (e.g., "Enter", "Esc", "Tab")
key_metaRequired. true if ALT key was pressed, false otherwise

Return Value

TypeDescription
eventReturns a keyboard event object with type = event.ev_keyboard

Usage

Create a keyboard event (typically done internally):

lua
import("core.ui.event")

-- This is typically created by the UI framework
-- when a key is pressed, but you can create one manually:
local e = event.keyboard(10, "Enter", false)

event.mouse

  • Create a mouse event object

Function Prototype

API

lua
event.mouse(btn_code: <number>, x: <number>, y: <number>, btn_name: <string>)

Parameter Description

ParameterDescription
btn_codeRequired. Mouse button event code
xRequired. X coordinate
yRequired. Y coordinate
btn_nameRequired. Button name string

Return Value

TypeDescription
eventReturns a mouse event object with type = event.ev_mouse

Usage

Create a mouse event (typically done internally):

lua
import("core.ui.event")

-- This is typically created by the UI framework
-- when mouse is used, but you can create one manually:
local e = event.mouse(1, 10, 20, "btn_left")

event.command

  • Create a command event object

Function Prototype

API

lua
event.command(command: <string>, extra?: <table>)

Parameter Description

ParameterDescription
commandRequired. Command name string
extraOptional. Additional data table

Return Value

TypeDescription
eventReturns a command event object with type = event.ev_command

Usage

Create and send a command event:

lua
import("core.ui.event")
import("core.ui.action")

-- Create a quit command event
local e = event.command("cm_quit")

-- Send the event to the view
view:on_event(e)

event.idle

  • Create an idle event object

Function Prototype

API

lua
event.idle()

Parameter Description

No parameters

Return Value

TypeDescription
eventReturns an idle event object with type = event.ev_idle

Usage

Create an idle event (typically done internally):

lua
import("core.ui.event")

-- This is typically created by the UI framework
-- when there's no user input:
local e = event.idle()