Skip to content

Love2D events

Tecs provides type-safe event wrappers for Love2D callbacks, allowing you to observe and react to window, input, and system events through the event system. Love2D events are automatically captured and emitted by Tecs when you use the framework. You can observe these events on the world or use traditional Love2D callbacks.

World-level events use address 0

Love2D events are world-level events, so use address 0 when observing them. See Events for more on address types.

Event flow

When an event is emitted from Love2D, Tecs emits the event to the Tecs event system and then emits the event to any registered love.* function. This means you can use both approaches:

teal
local tecs2d = require("tecs2d")

-- Tecs events work
world:observe(0, tecs2d.MousePressed, function(e: tecs2d.MousePressed)
    handleClick(e.x, e.y, e.button)
end)

-- Love2D callbacks work too
function love.mousepressed(x, y, button, istouch, presses)
    handleClick(x, y, button)
end

Available events

Window events

Focus

Triggered when the window gains or loses focus. See love.focus

PropertyTypeDescription
visiblebooleantrue if the window has focus
teal
world:observe(0, tecs2d.Focus, function(e: tecs2d.Focus)
    if e.visible then
        resumeGame()
    else
        pauseGame()
    end
end)

MouseFocus

Triggered when the window gains or loses mouse focus. See love.mousefocus

PropertyTypeDescription
focusbooleantrue if the window has mouse focus
teal
world:observe(0, tecs2d.MouseFocus, function(e: tecs2d.MouseFocus)
    if not e.focus then
        stopDragging()
    end
end)

Resize

Triggered when the window is resized. See love.resize

PropertyTypeDescription
widthnumberNew width of the window
heightnumberNew height of the window
teal
world:observe(0, tecs2d.Resize, function(e: tecs2d.Resize)
    camera:updateViewport(e.width, e.height)
end)

Visible

Triggered when the window is shown or hidden. See love.visible

PropertyTypeDescription
visiblebooleantrue if the window is visible
teal
world:observe(0, tecs2d.Visible, function(e: tecs2d.Visible)
    if not e.visible then
        audio:pauseMusic()
    else
        audio:resumeMusic()
    end
end)

Exposed

Triggered when the window is exposed and needs to be redrawn. See love.exposed

No properties.

Occluded

Triggered when the window is fully occluded by another window. See love.occluded

No properties.

Keyboard events

KeyPressed

Triggered when a key is pressed. See love.keypressed

PropertyTypeDescription
keylove.keyboard.KeyConstantThe key that was pressed (e.g., "space", "a")
scancodelove.keyboard.ScancodeHardware scancode for the key
isrepeatbooleanWhether this is a repeat event. Delay depends on user's system settings.
teal
world:observe(0, tecs2d.KeyPressed, function(e: tecs2d.KeyPressed)
    if e.key == "escape" then
        pauseGame()
    end
end)

KeyReleased

Triggered when a key is released. See love.keyreleased

PropertyTypeDescription
keylove.keyboard.KeyConstantThe key that was released
scancodelove.keyboard.ScancodeHardware scancode for the key
teal
world:observe(0, tecs2d.KeyReleased, function(e: tecs2d.KeyReleased)
    if e.key == "space" then
        endChargeAttack()
    end
end)

Mouse events

MousePressed

Triggered when a mouse button is pressed. See love.mousepressed

PropertyTypeDescription
xnumberMouse x position in pixels
ynumberMouse y position in pixels
buttonnumberButton index (1 = left, 2 = right, 3 = middle)
istouchbooleantrue if from a touchscreen
pressesnumberNumber of clicks for double/triple-click detection
teal
world:observe(0, tecs2d.MousePressed, function(e: tecs2d.MousePressed)
    if e.button == 1 and e.presses == 2 then
        handleDoubleClick(e.x, e.y)
    end
end)

MouseReleased

Triggered when a mouse button is released. See love.mousereleased

PropertyTypeDescription
xnumberMouse x position in pixels
ynumberMouse y position in pixels
buttonnumberButton index (1 = left, 2 = right, 3 = middle)
istouchbooleantrue if from a touchscreen
pressesnumberNumber of clicks
teal
world:observe(0, tecs2d.MouseReleased, function(e: tecs2d.MouseReleased)
    if e.button == 1 then
        endDragOperation(e.x, e.y)
    end
end)

Joystick events

JoystickAdded

Triggered when a joystick/gamepad is connected. See love.joystickadded

PropertyTypeDescription
joysticklove.joystick.JoystickThe newly connected joystick
teal
world:observe(0, tecs2d.JoystickAdded, function(e: tecs2d.JoystickAdded)
    if e.joystick:isGamepad() then
        setupGamepadMappings(e.joystick)
    end
end)

JoystickRemoved

Triggered when a joystick/gamepad is disconnected. See love.joystickremoved

PropertyTypeDescription
joysticklove.joystick.JoystickThe now-disconnected joystick
teal
world:observe(0, tecs2d.JoystickRemoved, function(e: tecs2d.JoystickRemoved)
    if love.joystick.getJoystickCount() == 0 then
        switchToKeyboardControls()
    end
end)

File events

DirectoryDropped

Triggered when a directory is dragged and dropped onto the window. See love.directorydropped

PropertyTypeDescription
pathstringFull platform-dependent directory path
xnumberX position where the directory was dropped
ynumberY position where the directory was dropped
teal
world:observe(0, tecs2d.DirectoryDropped, function(e: tecs2d.DirectoryDropped)
    love.filesystem.mount(e.path, "dropped")
    loadImagesFromDirectory("dropped")
end)

FileDropped

Triggered when a file is dragged and dropped onto the window. See love.filedropped

PropertyTypeDescription
filelove.filesystem.DroppedFileThe unopened file that was dropped
xnumberX position where the file was dropped
ynumberY position where the file was dropped
teal
world:observe(0, tecs2d.FileDropped, function(e: tecs2d.FileDropped)
    local filename = e.file:getFilename()
    if filename:match("%.png$") or filename:match("%.jpg$") then
        loadDroppedImage(e.file)
    end
end)

Application events

Quit

Triggered when the application is about to close. See love.quit

PropertyTypeDescription
exitstatusintegerThe exit code
teal
world:observe(0, tecs2d.Quit, function(e: tecs2d.Quit)
    saveGameState()
    cleanupResources()
end)

LocaleChanged

Triggered when the system locale changes. See love.localechanged

No properties.

ThemeChanged

Triggered when the system theme changes. See love.themechanged

PropertyTypeDescription
themestringThe new theme ("light" or "dark")
teal
world:observe(0, tecs2d.ThemeChanged, function(e: tecs2d.ThemeChanged)
    updateUITheme(e.theme)
end)

AudioDisconnected

Triggered when the audio device is disconnected. See love.audiodisconnected

No properties.

Touch events

TouchPressed

Triggered when a touch press is detected. See love.touchpressed

PropertyTypeDescription
idanyIdentifier for the touch press
xnumberX position of the touch
ynumberY position of the touch
pressurenumberPressure being applied (0-1)
deviceTypelove.touch.TouchDeviceTypeType of touchscreen or touchpad
isMousebooleantrue if from mouse emulation
teal
world:observe(0, tecs2d.TouchPressed, function(e: tecs2d.TouchPressed)
    handleTouch(e.id, e.x, e.y)
end)

TouchMoved

Triggered when a touch point moves. See love.touchmoved

PropertyTypeDescription
idanyIdentifier for the touch press
xnumberX position of the touch
ynumberY position of the touch
dxnumberX component of movement delta
dynumberY component of movement delta
pressurenumberPressure being applied (0-1)
deviceTypelove.touch.TouchDeviceTypeType of touchscreen or touchpad
isMousebooleantrue if from mouse emulation
teal
world:observe(0, tecs2d.TouchMoved, function(e: tecs2d.TouchMoved)
    handleTouchDrag(e.id, e.dx, e.dy)
end)

TouchReleased

Triggered when a touch point is released. See love.touchreleased

PropertyTypeDescription
idanyIdentifier for the touch press
xnumberX position of the touch
ynumberY position of the touch
pressurenumberPressure being applied (0-1)
deviceTypelove.touch.TouchDeviceTypeType of touchscreen or touchpad
isMousebooleantrue if from mouse emulation
teal
world:observe(0, tecs2d.TouchReleased, function(e: tecs2d.TouchReleased)
    handleTouchRelease(e.id, e.x, e.y)
end)

Sensor events

SensorUpdated

Triggered when a device sensor is updated. See love.sensorupdated

PropertyTypeDescription
sensorTypelove.sensor.SensorTypeType of sensor ("accelerometer" or "gyroscope")
xnumberX component of sensor data
ynumberY component of sensor data
znumberZ component of sensor data
teal
world:observe(0, tecs2d.SensorUpdated, function(e: tecs2d.SensorUpdated)
    handleSensor(e.sensorType, e.x, e.y, e.z)
end)

JoystickSensorUpdated

Triggered when a joystick sensor is updated. See love.joysticksensorupdated

PropertyTypeDescription
joysticklove.joystick.JoystickThe joystick with the sensor
sensorTypelove.joystick.SensorTypeType of sensor ("accelerometer" or "gyroscope")
xnumberX component of sensor data
ynumberY component of sensor data
znumberZ component of sensor data
teal
world:observe(0, tecs2d.JoystickSensorUpdated, function(e: tecs2d.JoystickSensorUpdated)
    handleJoystickSensor(e.joystick, e.sensorType, e.x, e.y, e.z)
end)

Drag-and-drop lifecycle events

DropBegan

Triggered when a drag operation begins over the window.

PropertyTypeDescription
xnumberX position where drag started
ynumberY position where drag started
teal
world:observe(0, tecs2d.DropBegan, function(e: tecs2d.DropBegan)
    showDropZone()
end)

DropMoved

Triggered when a drag operation moves over the window.

PropertyTypeDescription
xnumberCurrent drag x position
ynumberCurrent drag y position
teal
world:observe(0, tecs2d.DropMoved, function(e: tecs2d.DropMoved)
    updateDropHighlight(e.x, e.y)
end)

DropCompleted

Triggered when a drag operation is completed over the window.

PropertyTypeDescription
xnumberX position where drop completed
ynumberY position where drop completed
teal
world:observe(0, tecs2d.DropCompleted, function(e: tecs2d.DropCompleted)
    hideDropZone()
end)