pxt-calliope/docs/reference/input/button-is-pressed.md
2016-04-18 08:33:09 -07:00

981 B

Button Is Pressed

Get the state of an input button. The micro:bit has two input buttons: A and B.

input.buttonIsPressed(Button.A);

Parameters

  • name - String; input button "A", "B", or "A+B" (both input buttons)

Returns

  • Boolean - true if pressed, false if not pressed

Example

The following code uses an if statement to run code, depending on whether or not the A button is pressed:

basic.forever(() => {
    let pressed = input.buttonIsPressed(Button.A)
    if (pressed) {
        // this code runs if the A button is pressed
        basic.showNumber(1, 150)
    } else {
        // this code runs if the A button is *not* pressed
        basic.showNumber(0, 150)
    }
})

Lessons

zoomer

See also

on button pressed, if, forever