LoginLogin
Might make SBS readonly: thread

BUTTON

Root / Documentation / [.]

Created:
Check the status of a controller button.

Check Button

Check a specific button if it has been held, repeated, pressed, or released.

Syntax

state% = BUTTON(controller%, button% {, mode% })
BUTTON controller%, button% {, mode%} OUT state%
InputDescription
controller%ID of the controller to check; 0 to 4. Controller 0 is a combination of all connected controllers.
button%ID of the button to check; see Button IDs table.
mode%Button test mode (optional):
NumberDescription
0Button is held (default)
1Button was just pressed or triggered by repeat
2Button was just pressed
3Button was just released
OutputDescription
state%True if the button is in the checked state, false otherwise.

Examples

'turn the background red if the A button is held down
LOOP
 VSYNC
 IF BUTTON(0,#B_A) THEN
  BACKCOLOR #C_RED
 ELSE
  BACKCOLOR #C_CLEAR
 ENDIF
ENDLOOP

Button Bitset (SB3 Mode)

Return the state of all buttons as a bitset. This form is similar to SmileBASIC 3's BUTTON function.

Syntax

state% = BUTTON(controller%)
state% = BUTTON(controller%, -1, {, mode% })
BUTTON controller% OUT state%
BUTTON controller%, -1 {, mode%} OUT state%
InputDescription
controller%ID of the controller to check; 0 to 4. Controller 0 is a combination of all connected controllers.
-1Pass -1 as the button ID to use this mode (optional)
mode%Button test mode (optional):
NumberDescription
0Button is held (default)
1Button was just pressed or triggered by repeat
2Button was just pressed
3Button was just released
OutputDescription
state%Bitset of all buttons. Corresponding bit is set if state check is true, clear otherwise.

Examples

This function is used to return all of the button states simultaneously, for compatibility with previous SmileBASIC versions. However, the button ID constants are not compatible, so they must be used with a left-shift.
'turn the background red if the A button is held down
LOOP
 VSYNC
 IF BUTTON(0) AND 1<<#B_A THEN
  BACKCOLOR #C_RED
 ELSE
  BACKCOLOR #C_CLEAR
 ENDIF
ENDLOOP

Button IDs

This table contains the button IDs and their constants.
NameValueDescription
#B_RUP, #B_X0Top face button / X
#B_RDOWN, #B_B1Bottom face button / B
#B_RLEFT, #B_Y2Left face button / Y
#B_RRIGHT, #B_A3Right face button / A
#B_LUP4D-Pad up
#B_LDOWN5D-Pad down
#B_LLEFT6D-Pad left
#B_LRIGHT7D-Pad right
#B_L1, #B_SL8L trigger / SL trigger
#B_R1, #B_SR9R trigger / SR trigger
#B_L2, #B_S110ZL trigger / Joy-Con side trigger
#B_R2, #B_S211ZR trigger / Joy-Con side Z trigger
#B_LSTICK12Left stick click
#B_RSTICK13Right stick click
#B_RANY14Any right side button
#B_LANY15Any left side button
#B_ANY16Any button

See Also

  • CONTROLLER
  • BREPEAT
  • XCTRLSTYLE

No posts yet (will you be the first?)