Skip to main content

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
  2. Touch input
  3. Complete API index

1. Button Input​

btn([b]) → boolean | bitmask​

Gets the button state (pressed/released).

Parameters:

ParameterTypeDescription
bnumberButton number 0..7

Returns:

  • With a button number: true/false indicating 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:

ParameterTypeDescription
delaynumberDelay in frames before repeating begins
intervalnumberRepeat 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 only btn() 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 on btn(id, 1).

2. Touch Input​

inputcfg(opt) → previous_opt​

Configures touch and button functionality.

opt bit definitions:

BitDescription
bit0Enable touch mode
bit1Enable 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:

ReturnTypeDescription
x, ynumberTouch position
statnumberTouch state (0 = released; x/y then contain the last release position)
hotidnumberHot-zone number (note: always 0 on some hosts/builds; rectidx is more reliable)
rectidxnumberRectangle subregion number within the hot zone
abortnumberReserved; 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:

ParameterTypeDescription
hotidnumberHot-zone number 1..n. The same number can be defined multiple times (union of regions)
flagnumber1 = consume the touch event (do not pass it to the background framework)
x0, y0, x1, y1number/tableHot-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)
rectidxnumberSubregion 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​

FunctionReturnsDescription
btn([b])boolean / bitmaskButton state
btnp([b])boolean / bitmaskButton press (with repeat)
btnpcfg(delay, interval)old_delay, old_intervalConfigure button repeat
inputcfg(opt)previous_optConfigure input mode
touch()x, y, stat, hotid, rectidx, abortTouch state
touchdef(hotid, flag, x0, y0, x1, y1 [, rectidx])countDefine a hot zone
touchdef()—Clear all hot zones