pxt-calliope/docs/reference/input/button-is-pressed.md

44 lines
1.1 KiB
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# Button Is Pressed
Check whether a button is pressed right now. The micro:bit has two buttons: button `A` and button `B`.
2016-03-26 00:47:20 +01:00
```sig
input.buttonIsPressed(Button.A);
```
### Parameters
* ``name`` is a [String](/reference/types/string). You should store `A` in it to check the left button, `B` to check the right button, or `A+B` to check both at the same time.
2016-03-26 00:47:20 +01:00
### Returns
* [Boolean](/reference/types/boolean) that is `true` if the button you are checking is pressed, `false` if it is not pressed.
2016-03-26 00:47:20 +01:00
### Example
This program uses an [if](/reference/logic/if) to run
one part of the program if the `A` button is pressed, and
another part if it is not pressed.
2016-03-26 00:47:20 +01:00
```blocks
basic.forever(() => {
let pressed = input.buttonIsPressed(Button.A)
if (pressed) {
// this part runs if the A button is pressed
2016-03-26 00:47:20 +01:00
basic.showNumber(1, 150)
} else {
// this part runs if the A button is *not* pressed
2016-03-26 00:47:20 +01:00
basic.showNumber(0, 150)
}
})
```
### Lessons
2016-04-13 17:27:45 +02:00
[zoomer](/lessons/zoomer)
2016-03-26 00:47:20 +01:00
### See also
2016-04-18 17:33:09 +02:00
[on button pressed](/reference/input/on-button-pressed), [if](/reference/logic/if), [forever](/reference/basic/forever)
2016-03-26 00:47:20 +01:00