btnp( [i,] [p] )

Tests if a button has just been pressed, with keyboard-style repeating.

The button number.
The player number.

The btnp() function is similar to btn() except that it only reports that a button is on if it was not pressed during the previous frame. In other words, it returns true only if a given button was pressed just now, and does not report true again for the same button in the next frame even if it is held down.

This is useful for detecting button presses that are meant to select menu options or initiate actions. If you were to use btn() to initiate an action, the player is likely to initiate the action multiple times that they didn't intend, because the button will register as on for multiple frames.

btnp() implements a keyboard-like repeat mechanism: if the player holds the button for 15 frames, it registers as on again for one frame, then again every four frames after that. The frame counter resets when the player releases the button.

Like btn(), btnp() can take a button number and a player number and return true or false, or can be called without arguments to return a bitfield of button states.

See btn() for a description of the arguments and return values.

Examples Edit

sel = 0
items = {"a", "b", "c", "d",
         "e", "f", "g", "h"}
function _update()
  if (btnp(0) and sel > 0) sel -= 1
  if (btnp(1) and sel < #items - 1) sel += 1
function _draw()
  for i=1,#items do
    print(items[i], 10 * i, 10, 7)
  rect(8 + (sel * 10), 8,
       14 + (sel * 10), 16,

Try replacing btnp() with btn() to see what happens.

See also Edit