btn( [i,] [p] )

Tests if a button is being pressed at this moment.

The button number.
The player number.

Pico-8 supports six buttons (0-5) of input for each of up to eight players (0-7). When on the device with a keyboard, inputs for players 0 and 1 can come from keys on the keyboard. All eight players can connect SDL-supported controllers. Each controller is meant to be (or resemble) a Nintendo Entertainment System controller.

btn() called with both arguments returns true if the given player is pressing the given button, false otherwise. If the button number is provided but the player number is omitted, player 0 is assumed.

When btn() is called without arguments, it returns a bitfield of the button states for players 0 and 1. Player 0's buttons are represented by bits 0 through 5 (the rightmost bits), and player 1's buttons are represented by bits 6 through 11 (the leftmost bits). Each bit corresponds to a button number, and a 1 (on) bit representing that the button is pressed.

The button numbers and default keyboard emulation keys are as follows:

Number Button Player 0 key Player 1 key
0 left left arrow S
1 right right arrow F
2 up up arrow E
3 down down arrow D
4 button "o" Z, C, or N left Shift or Tab
5 button "x" X, V, or M A or Q

btn() is useful for most action/arcade button uses, where holding down a button for multiple frames needs to be detected for each frame. For actions such as selecting menu options, see btnp().

Examples Edit

x = 0
y = 0
c = 8
function _update()
  if (btn(0) and x > 0) x -= 1
  if (btn(1) and x < 127) x += 1
  if (btn(2) and y > 0) y -= 1
  if (btn(3) and y < 127) y += 1
  if (btn(4) and c > 1) c -= 1
  if (btn(5) and c < 15) c += 1
function _draw()
  circfill(x, y, 10, c)

See also Edit