rb.on runs a function when something happens to the script or to the input
it sees: the script loading or unloading, its window gaining or losing focus, a
tick, a key, a mouse movement or a controller stick. A script reaches for it to set up
state at start, clean up at stop, or react to input as it arrives.
This script counts key and button presses without blocking any of them and prints the
total when it unloads:
rb.on(event: Event, fn: (...any) -> boolean?) -> EventHandle
Adds a handler for an event and returns an EventHandle whose stop
removes it. A script may add several handlers for one event; they run in the
order they were added. A handler that raises stops the handlers after it for
that event.
Errors: an unknown name raises rb.on: unknown event '<name>'. Valid events:
followed by the list; a handler that is not a function raises
rb.on: the handler must be a function.
Lifecycle
window is { title, process, x, y, width, height }.
Top-level code runs first, when the file loads; start follows. A script with no
targeting is active from then on. A targeted script whose target is
focused at load gets focus right after start; otherwise it waits inactive.
While a script is inactive it gets no tick, and its timers and coroutines wait.
Keys and buttons a waiting coroutine holds through rb.hid stay held in
whatever window has focus. Stop the task and release them in a blur handler:
tick runs every 8000 / tick_rate
engine ticks (integer division), so the default tick_rate=1000 runs it every
8th tick. dt_ms is the real time since this script’s previous tick, in
fractional milliseconds, and 0 on the first. Timers and coroutines are
serviced on every engine tick; HTTP, WebSocket and UDP servers every 8th.
An error raised in start, focus, blur or tick stops the script with that
error. An error in stop is logged.
This handler counts ticks and removes itself after the 1,000th:
Input events
key is a name from the Key reference; device is described
under Devices. Controller buttons arrive as down and up with
names such as "PadA"; see Controllers.
A handler returns false to block the event. Any other return, or none, lets it
through. Active scripts see an event in z_index order, highest
first. Within one script, its binds are tried first, then its handlers.
The first false, or a bind that takes the key, ends the event: lower scripts do
not see it. An error in an input handler is logged, and the event continues as
if that script had not blocked it.
Exclusive capture. A down or up handler can block any key, so while a
script with one is active, Rebind intercepts every key and re-emits the ones that
pass. Use
rb.bind to take only the keys you name, or declare
key_block=false to observe keys without intercepting them.
Mouse movement. A move handler and mouse_block=true need hardware mode.
In software mode Rebind refuses to start a script with either, and an autostart
script waits for hardware mode; an rb.on("move", ...) that runs in software
mode raises rb.on: a move handler needs hardware mode. Without mouse_block=true, movement is
forwarded at once and move handlers run when the engine is free; their return
value is ignored.
Controller axes. An axis handler runs once for each stick or trigger that
changed, at most once per controller report. axis is "LeftX", "LeftY",
"RightX", "RightY" (-1 to 1, up and left negative) or "LT", "RT" (0 to
1). Returning false keeps that value off Rebind Link’s joypad; the other axes
of the same report still pass. A value a handler swallows while the
controller had already moved that axis returns the axis to rest on the joypad.
Controllers are captured only in hardware mode. An axis handler needs
min_sdk=3.11.0.
Observer events
Every key, button, move and scroll is also delivered as an observer event to every active script, before the blocking handlers run.input_move follows the
same mouse_block rule as move. Observers cannot block: their return value is
ignored. A script that uses only observers takes no exclusive capture.
Controller axes have no observer event. A script that watches sticks uses
axis
and returns nothing; a higher z_index script whose axis handler returns
false hides that value from it.
Devices
Input handlers, observers, andrb.bind callbacks receive the device the input
came from:
device is nil in software mode, where the source cannot tell devices
apart.
rb.input.devices() lists the devices Rebind has identified, one
entry per id (see exact), with these fields; it needs min_sdk=3.12.0. A device that is both a
keyboard and a mouse lists as its keyboard. A
device attached a moment ago may not be listed yet, so call it when you need the
answer rather than once at start. From a terminal:
Not dispatched
rb.on accepts "scroll_h" (delta), "reload" (no arguments) and "error"
(message), but Rebind never dispatches them. A handler for one
raises no error and never runs.