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 aliases —
util.input.key.LSHIFT,RSHIFT,LCTRL,RCTRL,LALT,RALT,LMETA,RMETAall resolve to their non-sided counterpart.
Parameters
| Type | Name | Description |
|---|---|---|
util.input.key | key | Key or button to listen to: • Refer util.input.key enum |
string | direction | Input phase to trigger on: • When "down" - triggers while the key is held• When "up" - triggers when the key is released |
function | exec | Handler function to invoke when the input fires |
Returns
| Type | Name | Description |
|---|---|---|
bool | status | true on successful registration, false otherwise |
Callback: exec
exec(key, direction)| Type | Name | Description |
|---|---|---|
int | key | Key enum value of the key or button that triggered the handler |
string | direction | Input phase that fired: • When "down" - triggers while the key is held• When "up" - triggers when the key is released |
Examples
util.input.bind(util.input.key.SPACE, "down", function(key, direction)
core.engine.print("info", "Space pressed")
end)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)util.input.bind(util.input.key.MOUSE_RIGHT, "down", function(key, direction)
core.engine.print("info", "Right mouse button pressed")
end)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)