tecs.input

Keyboard, pointer, touch and gamepad state folded from typed platform events.

Live state survives frame boundaries. Frame edges clear before each sealed host batch, while latched edges accumulate until the first fixed step consumes them. Blocking layers move capture and clear pending edges without inventing releases for controls that remain physically held.

Gamepads reach the same fold through a different door. The keyboard, the pointer and touch arrive in the host's sealed batch, because the window system owns them; a gamepad belongs to no window, so tecs.platform.gamepadbackend is polled once a frame by the tecs.PollGamepads system install adds, and what it saw becomes the same typed platform events the host would have sent. That is why handleEvent is the only fold in this module: a synthetic gamepad event from the debug server and a real one from a controller take one path.

Buttons use positional names. "south" is the button nearest the player on every pad, where "a" is the one nearest the player on some pads and the one to its right on others.

local pad <const> = state:gamepad(1)
if pad ~= nil and pad.connected then
    local moveX <const> = pad:axis("leftX")
    if pad:buttonPressed("south") then
        print(moveX)
    end
end

Device support#

Gamepads expose positional buttons, sticks, analog triggers, hot plug, power state, and rumble. Rumble is conditional: gilrs implements force feedback on evdev and XInput but not on IOKit, so Gamepad.rumble answers false on macOS and for devices without motors.

Touch is available on the platforms where winit reports it: Windows, Linux, Android, and the web. Touch pressure comes through where the surface measures it and reads zero where it does not.

The pen is the pointer. winit declares no pen event, so a stylus arrives as the cursor moving and its buttons as mouse buttons, and Tecs reads it that way rather than inventing a device the layer below does not report. A game that draws with a stylus reads pointerX, pointerY and the mouse buttons and gets what the platform sends.

Pen pressure, tilt, and eraser state are not exposed because winit declares no pen event. Supporting them would require separate NSEvent, WM_POINTER, and XInput2 tablet handling.

Gamepad motion sensors, touchpads, trigger rumble, light bars, player indexes, hardware button labels, device families, device paths, remap events, and standalone motion sensors are not part of the input surface. A game that wants a "cross" glyph rather than a "south" name reads the pad's name and chooses its own art.

Module contents

Types

TypeKindDescription
GamepadrecordOne attached gamepad's identity, event state and outputs.
GamepadAxistypeA gamepad axis name.
GamepadButtontypeA positional gamepad button name.
InputrecordLive, frame-edge, and fixed-latched keyboard and pointer input.
LayerrecordA position in the input capture stack.
MouseButtontypeA conventional pointer button name.
TouchrecordOne finger on a touch surface, from the most recent touch event.

Functions

FunctionKindDescription
installfunctionInstalls and returns the input state owned by one world.
newInputfunctionCreates empty input state with the readable base layer.
offunctionReturns the input state installed in a world.

Types#

Gamepadrecord#

record Gamepad ...

One attached gamepad's identity, event state and outputs.

Input owns the object. A retained reference stays safe after the device goes away: queries answer with neutral values, rumble returns false, and connected stays false. A reconnect creates a new object rather than reviving the old one.

Members

NameKindDescription
idfieldRead-only.
connectedfieldRead-only.
namefieldRead-only.
guidfieldRead-only.
backendfield
gateReadmethodReads the shared layer stack.
gateFixedmethodReports whether queries should answer with fixed-step latched edges.
downfield
pressedfield
releasedfield
latchedPressedfield
latchedReleasedfield
axisValuesfieldLatest value per axis.
buttonDownmethod
buttonPressedmethod
buttonReleasedmethod
axismethod
hasButtonmethod
hasAxismethod
powermethod
rumblemethod

idfield#

id: integer

Read-only. Names the device, and matches the which field of every gamepad platform event about it.

connectedfield#

connected: boolean

Read-only. Reports true from the moment Input opens the device until it goes away, and false permanently afterwards.

namefield#

name: string

Read-only. Reports the platform's display name for the device, and an empty string when it reports none. Saved bindings use guid.

guidfield#

guid: string

Read-only. Reports the stable identity a saved binding matches on, and an empty string when the platform reports none.

backendfield#

backend: gamepadbackend.Backend
@private

gateReadmethod#

gateRead: function(layer: Layer?): boolean
@private

Reads the shared layer stack. A layer blocks every device or none, so this answers from the owning input state rather than from the pad.

Arguments
NameTypeDescription
layerLayer?
Returns
TypeDescription
boolean

gateFixedmethod#

gateFixed: function(): boolean
@private

Reports whether queries should answer with fixed-step latched edges.

Returns
TypeDescription
boolean

downfield#

down: {[integer]: boolean}
@private

pressedfield#

pressed: {[integer]: boolean}
@private

releasedfield#

released: {[integer]: boolean}
@private

latchedPressedfield#

latchedPressed: {[integer]: boolean}
@private

latchedReleasedfield#

latchedReleased: {[integer]: boolean}
@private

axisValuesfield#

axisValues: {[integer]: number}
@private

Latest value per axis. An axis no event has mentioned reads as zero.

buttonDownmethod#

buttonDown: function(borrows self: Gamepad, button: GamepadButton | integer, layer: Layer?): boolean
Arguments
NameTypeDescription
borrows selfGamepad
buttonGamepadButton | integer
layerLayer?
Returns
TypeDescription
boolean

buttonPressedmethod#

buttonPressed: function(borrows self: Gamepad, button: GamepadButton | integer, layer: Layer?): boolean
Arguments
NameTypeDescription
borrows selfGamepad
buttonGamepadButton | integer
layerLayer?
Returns
TypeDescription
boolean

buttonReleasedmethod#

buttonReleased: function(borrows self: Gamepad, button: GamepadButton | integer, layer: Layer?): boolean
Arguments
NameTypeDescription
borrows selfGamepad
buttonGamepadButton | integer
layerLayer?
Returns
TypeDescription
boolean

axismethod#

axis: function(borrows self: Gamepad, axis: GamepadAxis | integer, deadzone: number?, layer: Layer?): number
Arguments
NameTypeDescription
borrows selfGamepad
axisGamepadAxis | integer
deadzonenumber?
layerLayer?
Returns
TypeDescription
number

hasButtonmethod#

hasButton: function(borrows self: Gamepad, button: GamepadButton | integer): boolean
Arguments
NameTypeDescription
borrows selfGamepad
buttonGamepadButton | integer
Returns
TypeDescription
boolean

hasAxismethod#

hasAxis: function(borrows self: Gamepad, axis: GamepadAxis | integer): boolean
Arguments
NameTypeDescription
borrows selfGamepad
axisGamepadAxis | integer
Returns
TypeDescription
boolean

powermethod#

power: function(borrows self: Gamepad): (gamepadbackend.PowerState, integer)
Arguments
NameTypeDescription
borrows selfGamepad
Returns
TypeDescription
gamepadbackend.PowerState
integer

rumblemethod#

rumble: function(borrows self: Gamepad, low: number, high: number, seconds: number): boolean
Arguments
NameTypeDescription
borrows selfGamepad
lownumber
highnumber
secondsnumber
Returns
TypeDescription
boolean

GamepadAxistype#

type GamepadAxis = "leftX" | "leftY" | "rightX" | "rightY" | "leftTrigger" | "rightTrigger"

A gamepad axis name.

Compatibility surface: these names are stable. A stick runs -1 to 1 with leftY and rightY growing downward, and a trigger runs zero to one.

GamepadButtontype#

type GamepadButton = "south"
| "east"
| "west"
| "north"
| "back"
| "guide"
| "start"
| "leftStick"
| "rightStick"
| "leftShoulder"
| "rightShoulder"
| "dpadUp"
| "dpadDown"
| "dpadLeft"
| "dpadRight"

A positional gamepad button name.

These are what gilrs reports, spelled as Tecs publishes them. They are positional rather than Xbox-only because the hardware label is not part of the input contract. Names without a gilrs counterpart are not published.

Inputrecord#

record Input ...

Live, frame-edge, and fixed-latched keyboard and pointer input.

Members

NameKindDescription
mouseXfield
mouseYfield
mouseDeltaXfield
mouseDeltaYfield
wheelXfield
wheelYfield
wheelTicksXfield
wheelTicksYfield
textfield
keysDownfield
keysPressedfield
keysReleasedfield
keysLatchedPressedfield
keysLatchedReleasedfield
mouseButtonsDownfield
mouseButtonsPressedfield
mouseButtonsReleasedfield
mouseButtonsLatchedPressedfield
mouseButtonsLatchedReleasedfield
modifierMaskfield
fixedfield
layersfield
enterFixedSystemfield
exitFixedSystemfield
pollSystemfield
backendfield
padsfield
padListfield
padEventsfieldThe drain buffer, allocated once because the seam is crossed per frame.
fingersfield
beginFramemethod
enterFixedPhasemethod
exitFixedPhasemethod
handleEventmethod
keyDownmethod
keyPressedmethod
keyReleasedmethod
modifiersmethod
mouseDownmethod
mousePressedmethod
mouseReleasedmethod
pushLayermethod
popLayermethod
topLayermethod
canReadmethod
touchesmethod
gamepadsmethod
gamepadmethod
gamepadByIdmethod
refreshDevicesmethod
pollDevicesmethod
destroymethod

mouseXfield#

mouseX: number

mouseYfield#

mouseY: number

mouseDeltaXfield#

mouseDeltaX: number

mouseDeltaYfield#

mouseDeltaY: number

wheelXfield#

wheelX: number

wheelYfield#

wheelY: number

wheelTicksXfield#

wheelTicksX: integer

wheelTicksYfield#

wheelTicksY: integer

textfield#

text: string

keysDownfield#

keysDown: {[string]: boolean}
@private

keysPressedfield#

keysPressed: {[string]: boolean}
@private

keysReleasedfield#

keysReleased: {[string]: boolean}
@private

keysLatchedPressedfield#

keysLatchedPressed: {[string]: boolean}
@private

keysLatchedReleasedfield#

keysLatchedReleased: {[string]: boolean}
@private

mouseButtonsDownfield#

mouseButtonsDown: {[integer]: boolean}
@private

mouseButtonsPressedfield#

mouseButtonsPressed: {[integer]: boolean}
@private

mouseButtonsReleasedfield#

mouseButtonsReleased: {[integer]: boolean}
@private

mouseButtonsLatchedPressedfield#

mouseButtonsLatchedPressed: {[integer]: boolean}
@private

mouseButtonsLatchedReleasedfield#

mouseButtonsLatchedReleased: {[integer]: boolean}
@private

modifierMaskfield#

modifierMask: integer
@private

fixedfield#

fixed: boolean
@private

layersfield#

layers: {Layer}
@private

enterFixedSystemfield#

enterFixedSystem: ecs.System?
@private

exitFixedSystemfield#

exitFixedSystem: ecs.System?
@private

pollSystemfield#

pollSystem: ecs.System?
@private

backendfield#

backend: gamepadbackend.Backend
@private

padsfield#

pads: {[integer]: Gamepad}
@private

padListfield#

padList: {Gamepad}
@private

padEventsfield#

padEvents: {gamepadbackend.Event}
@private

The drain buffer, allocated once because the seam is crossed per frame.

fingersfield#

fingers: {[string]: Touch}
@private

beginFramemethod#

beginFrame: function(exclusive self: Input): nil
Arguments
NameTypeDescription
exclusive selfInput
Returns
TypeDescription
nil

enterFixedPhasemethod#

enterFixedPhase: function(exclusive self: Input): nil
Arguments
NameTypeDescription
exclusive selfInput
Returns
TypeDescription
nil

exitFixedPhasemethod#

exitFixedPhase: function(exclusive self: Input): nil
Arguments
NameTypeDescription
exclusive selfInput
Returns
TypeDescription
nil

handleEventmethod#

handleEvent: function(self: Input, event: platformevents.Event): nil
Arguments
NameTypeDescription
selfInput
eventplatformevents.Event
Returns
TypeDescription
nil

keyDownmethod#

keyDown: function(borrows self: Input, key: string, layer: Layer?): boolean
Arguments
NameTypeDescription
borrows selfInput
keystring
layerLayer?
Returns
TypeDescription
boolean

keyPressedmethod#

keyPressed: function(borrows self: Input, key: string, layer: Layer?): boolean
Arguments
NameTypeDescription
borrows selfInput
keystring
layerLayer?
Returns
TypeDescription
boolean

keyReleasedmethod#

keyReleased: function(borrows self: Input, key: string, layer: Layer?): boolean
Arguments
NameTypeDescription
borrows selfInput
keystring
layerLayer?
Returns
TypeDescription
boolean

modifiersmethod#

modifiers: function(borrows self: Input): integer
Arguments
NameTypeDescription
borrows selfInput
Returns
TypeDescription
integer

mouseDownmethod#

mouseDown: function(borrows self: Input, button: MouseButton | integer, layer: Layer?): boolean
Arguments
NameTypeDescription
borrows selfInput
buttonMouseButton | integer
layerLayer?
Returns
TypeDescription
boolean

mousePressedmethod#

mousePressed: function(borrows self: Input, button: MouseButton | integer, layer: Layer?): boolean
Arguments
NameTypeDescription
borrows selfInput
buttonMouseButton | integer
layerLayer?
Returns
TypeDescription
boolean

mouseReleasedmethod#

mouseReleased: function(borrows self: Input, button: MouseButton | integer, layer: Layer?): boolean
Arguments
NameTypeDescription
borrows selfInput
buttonMouseButton | integer
layerLayer?
Returns
TypeDescription
boolean

pushLayermethod#

pushLayer: function(exclusive self: Input, name: string, blocking: boolean?): Layer
Arguments
NameTypeDescription
exclusive selfInput
namestring
blockingboolean?
Returns
TypeDescription
Layer

popLayermethod#

popLayer: function(exclusive self: Input): Layer?
Arguments
NameTypeDescription
exclusive selfInput
Returns
TypeDescription
Layer?

topLayermethod#

topLayer: function(borrows self: Input): Layer
Arguments
NameTypeDescription
borrows selfInput
Returns
TypeDescription
Layer

canReadmethod#

canRead: function(borrows self: Input, layer: Layer?): boolean
Arguments
NameTypeDescription
borrows selfInput
layerLayer?
Returns
TypeDescription
boolean

touchesmethod#

touches: function(borrows self: Input, layer: Layer?): {Touch}
Arguments
NameTypeDescription
borrows selfInput
layerLayer?
Returns
TypeDescription
{Touch}

gamepadsmethod#

gamepads: function(borrows self: Input): {Gamepad}
Arguments
NameTypeDescription
borrows selfInput
Returns
TypeDescription
{Gamepad}

gamepadmethod#

gamepad: function(borrows self: Input, index: integer?): Gamepad?
Arguments
NameTypeDescription
borrows selfInput
indexinteger?
Returns
TypeDescription
Gamepad?

gamepadByIdmethod#

gamepadById: function(borrows self: Input, id: integer): Gamepad?
Arguments
NameTypeDescription
borrows selfInput
idinteger
Returns
TypeDescription
Gamepad?

refreshDevicesmethod#

refreshDevices: function(self: Input): nil
Arguments
NameTypeDescription
selfInput
Returns
TypeDescription
nil

pollDevicesmethod#

pollDevices: function(exclusive self: Input): {platformevents.Event}
Arguments
NameTypeDescription
exclusive selfInput
Returns
TypeDescription
{platformevents.Event}

destroymethod#

destroy: function(exclusive self: Input): nil
Arguments
NameTypeDescription
exclusive selfInput
Returns
TypeDescription
nil

Layerrecord#

record Layer ...

A position in the input capture stack.

Members

NameKindDescription
namefield
blockingfield
indexfield

namefield#

name: string

blockingfield#

blocking: boolean

indexfield#

index: integer

MouseButtontype#

type MouseButton = "left" | "middle" | "right" | "back" | "forward"

A conventional pointer button name.

These are winit's names. back and forward identify the two auxiliary pointer buttons.

Touchrecord#

record Touch ...

One finger on a touch surface, from the most recent touch event.

Input owns and reuses every field. Callers treat the record as read-only and copy values they need to retain.

Members

NameKindDescription
devicefieldRead-only.
fingerfieldRead-only.
xfieldRead-only.
yfieldRead-only.
normalXfieldRead-only.
normalYfieldRead-only.
pressurefieldRead-only.

devicefield#

device: string

Read-only. Names the touch surface's opaque identity, set when the finger first appears.

fingerfield#

finger: string

Read-only. Names the finger's opaque identity, set when it first appears.

xfield#

x: number

Read-only. Reports the position in logical window coordinates.

yfield#

y: number

Read-only. Reports the position in logical window coordinates.

normalXfield#

normalX: number

Read-only. Reports the position across the surface, from zero to one.

normalYfield#

normalY: number

Read-only. Reports the position down the surface, from zero to one.

pressurefield#

pressure: number

Read-only. Reports the pressure from zero to one, and zero where the surface does not measure it.

Functions#

installfunction#

function install(exclusive world: ecs.World, backend: gamepadbackend.Backend?): Input

Installs and returns the input state owned by one world.

Three systems come with it. Two consume the fixed-step latched edges, and tecs.PollGamepads runs in Ingress, ahead of everything a game schedules, so a controller read anywhere in the frame answers from this frame.

This is the call that reaches real hardware. The platform's device source is opened once per process and shared, because opening it per world would give one controller two identities on two hot-plug streams; a build with no libtecsgamepad gets the device-free source and says so once in the log.

Arguments

NameTypeDescription
exclusive worldecs.World

the world whose fixed steps consume latched edges

backendgamepadbackend.Backend?

the gamepad source, defaulting to the process-wide platform one

Returns

TypeDescription
Input

the new input state, also available through of

Raises

TypeCondition
string

when the world already owns input state

newInputfunction#

function newInput(backend: gamepadbackend.Backend?): Input

Creates empty input state with the readable base layer.

The state reads no device unless it is given a source. Constructing input state by hand is what a test and a headless tool do, and neither should start the platform's device enumeration as a side effect: on macOS that is a run loop on a thread of its own, and a suite whose result changes when somebody plugs a controller in is worse than one that reads no controller at all. install is the call that reaches real hardware.

Arguments

NameTypeDescription
backendgamepadbackend.Backend?

the gamepad source, defaulting to no devices at all

Returns

TypeDescription
Input

a standalone input state

offunction#

function of(borrows world: ecs.World): Input?

Returns the input state installed in a world.

Arguments

NameTypeDescription
borrows worldecs.World

the world to inspect

Returns

TypeDescription
Input?

its input state, or nil when none is installed