util.input.bind

Client

Registers a handler function that fires when a key or mouse button is pressed or released


Syntax

local status = util.input.bind(
    key,
    direction,
    exec
)

Handlers are scoped to the environment that registered them and are automatically cleaned up when that environment is destroyed.

  • Multiple handlers — the same key and direction combination can have any number of handlers registered independently.
  • Modifier aliasesutil.input.key.LSHIFT, RSHIFT, LCTRL, RCTRL, LALT, RALT, LMETA, RMETA all resolve to their non-sided counterpart.

Parameters

TypeNameDescription
util.input.keykeyKey or button to listen to:
• Refer util.input.key enum
stringdirectionInput phase to trigger on:
• When "down" - triggers while the key is held
• When "up" - triggers when the key is released
functionexecHandler function to invoke when the input fires

Returns

TypeNameDescription
boolstatustrue on successful registration, false otherwise

Callback: exec

exec(key, direction)
TypeNameDescription
intkeyKey enum value of the key or button that triggered the handler
stringdirectionInput phase that fired:
• When "down" - triggers while the key is held
• When "up" - triggers when the key is released

Examples

Bind a handler to the spacebar press
util.input.bind(util.input.key.SPACE, "down", function(key, direction)
    core.engine.print("info", "Space pressed")
end)
Bind separate handlers for press and release
util.input.bind(util.input.key.E, "down", function()
    core.engine.print("info", "E pressed - interact start")
end)

util.input.bind(util.input.key.E, "up", function()
    core.engine.print("info", "E released - interact end")
end)
Bind a mouse button handler
util.input.bind(util.input.key.MOUSE_RIGHT, "down", function(key, direction)
    core.engine.print("info", "Right mouse button pressed")
end)
Bind a handler and store a reference for later removal
local function on_jump(key, direction)
    core.engine.print("info", "Jump triggered")
end

util.input.bind(util.input.key.SPACE, "down", on_jump)

-- Remove when no longer needed
util.input.unbind(util.input.key.SPACE, "down", on_jump)

On this page