coloX Input API
coloX supports button and touch input. Some devices support only one of these. Touch devices enable touch and simulated button input by default.
Contents
1. Button Input
btn([b]) → boolean | bitmask
Gets the button state (pressed/released).
Parameters:
| Parameter | Type | Description |
|---|---|---|
b | number | Button number 0..7 |
Returns:
- With a button number:
true/falseindicating whether that button is pressed - Without arguments: the bitfield state of all buttons
-- Check a single button
if btn(0) then
y = y - 2 -- Up
end
-- Check all buttons
local keys = btn()
if band(keys, 0x01) ~= 0 then -- Button 0
-- ...
end
btnp([b]) → boolean | bitmask
Detects a button press: returns true when the button was released in the previous frame and is pressed in the current frame.
While held, it repeats every 4 frames after a 15-frame delay; configure this with btnpcfg. The SYS button (number 7) triggers for only one frame.
Without arguments, returns the bitmask for all buttons (as with btn()).
-- Trigger once (suitable for jumping, selecting, etc.)
if btnp(4) then
jump()
end
btnpcfg(delay, interval) → old_delay, old_interval
Configures button repeat parameters.
Parameters:
| Parameter | Type | Description |
|---|---|---|
delay | number | Delay in frames before repeating begins |
interval | number | Repeat interval in frames |
Returns: The previous (delay, interval) values.
btnpcfg(15, 4) -- Defaults: 15-frame delay, 4-frame interval
btnpcfg(255, 0) -- Disable auto-repeat
About Gamepads
Gamepads use a separate API, described in pad.md, enabled with usemod('pad').
There are just two points to remember about their relationship to this section:
- Gamepads drive
btn()/btnp(): player 0's gamepad buttons are merged into the button stream. With one gamepad connected, older cartridges using onlybtn()can be played with it without changes. - The second parameter (player) in
btn(id, p)currently supports only 0. For player 1 / player 2 input, use pad.md; do not rely onbtn(id, 1).
2. Touch Input
inputcfg(opt) → previous_opt
Configures touch and button functionality.
opt bit definitions:
| Bit | Description |
|---|---|
| bit0 | Enable touch mode |
| bit1 | Enable touch-to-button simulation |
inputcfg(0x01) -- Enable touch only
inputcfg(0x03) -- Touch + touch-to-button emulation
touch() → x, y, stat, hotid, rectidx, abort
Queries the touch state.
Return values:
| Return | Type | Description |
|---|---|---|
x, y | number | Touch position |
stat | number | Touch state (0 = released; x/y then contain the last release position) |
hotid | number | Hot-zone number (note: always 0 on some hosts/builds; rectidx is more reliable) |
rectidx | number | Rectangle subregion number within the hot zone |
abort | number | Reserved; currently always 0 |
function _update()
local x, y, stat, hotid = touch()
if stat ~= 0 then
-- Touch active at (x, y)
printh("Touch at " .. x .. "," .. y .. " hot:" .. hotid)
end
end
touchdef(hotid, flag, x0, y0, x1, y1 [, rectidx]) → count
Defines a touch hot zone. Call touchdef() without arguments to clear all hot zones.
Parameters:
| Parameter | Type | Description |
|---|---|---|
hotid | number | Hot-zone number 1..n. The same number can be defined multiple times (union of regions) |
flag | number | 1 = consume the touch event (do not pass it to the background framework) |
x0, y0, x1, y1 | number/table | Hot-zone rectangle coordinates. If x0 is a table, defines a polygon hot zone (3..8 vertices, a flat table of 6..16 numbers; an out-of-range vertex count invalidates the zone without an error) |
rectidx | number | Subregion number; default 1 |
Returns: The current total number of hot zones.
-- Define rectangular hot zones
touchdef(1, 1, 10, 10, 100, 50) -- Hot zone 1: consume events
touchdef(2, 0, 110, 10, 200, 50) -- Hot zone 2: do not consume events
-- Define a polygonal hot zone
touchdef(3, 1, {120, 10, 180, 60, 120, 110, 60, 60}) -- Diamond
-- Clear all hot zones
touchdef()
3. Complete API Index
| Function | Returns | Description |
|---|---|---|
btn([b]) | boolean / bitmask | Button state |
btnp([b]) | boolean / bitmask | Button press (with repeat) |
btnpcfg(delay, interval) | old_delay, old_interval | Configure button repeat |
inputcfg(opt) | previous_opt | Configure input mode |
touch() | x, y, stat, hotid, rectidx, abort | Touch state |
touchdef(hotid, flag, x0, y0, x1, y1 [, rectidx]) | count | Define a hot zone |
touchdef() | — | Clear all hot zones |