pxt-ev3/docs/reference/brick/button/is-pressed.md
Peli de Halleux e94ac6f6f1
renaming brick.setLight to brick.setStatusLight (#307)
* renaming brick.setLight to brick.setStatusLight

* updated docs
2018-02-06 22:18:39 -08:00

1.2 KiB

is Pressed

Check if a button is being pressed or not.

brick.buttonEnter.isPressed()

~hint

Touch sensors

Your @boardname@ has touch sensors that work like buttons. Instead of saying enter or left as the source button, use a touch sensor block with a sensor name like touch 1.

if (sensors.touch1.isPressed()) {
    console.log("Hey, I feel pressed.");
}

Read about touch sensors and using them as touch buttons.

~

Returns

  • a boolean: true if the button is pressed, false if the button is not pressed

Example

Set the brick light to green when the down is pressed. When the button is not pressed, the brick light is red.

let isRed = false;
loops.forever(function() {
    if (brick.buttonLeft.isPressed()) {
        brick.setStatusLight(StatusLight.Green);
        isRed = false;
    } else {
        if (!isRed) {
            brick.setStatusLight(StatusLight.Red);
            isRed = true;
        }
    }
})

See also

was pressed, on event

Touch sensors