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

42 lines
961 B
Markdown
Raw Normal View History

2016-03-26 00:47:20 +01:00
# Button Is Pressed
Get the state of an input button. The micro:bit has two input buttons: A and B.
```sig
input.buttonIsPressed(Button.A);
```
### Parameters
2016-04-13 17:27:45 +02:00
* name - [String](/reference/types/string); input button "A", "B", or "A+B" (both input buttons)
2016-03-26 00:47:20 +01:00
### Returns
2016-04-13 17:27:45 +02:00
* [Boolean](/reference/types/boolean) - `true` if pressed, `false` if not pressed
2016-03-26 00:47:20 +01:00
### Example
2016-04-13 17:27:45 +02:00
The following code uses an [if](/reference/logic/if) statement to run code, depending on whether or not the A button is pressed:
2016-03-26 00:47:20 +01:00
```blocks
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
2016-04-13 17:27:45 +02:00
[zoomer](/lessons/zoomer)
2016-03-26 00:47:20 +01:00
### See also
2016-04-13 17:27:45 +02:00
[on button pressed](/input/on-button-pressed), [if](/reference/logic/if), [forever](/basic/forever)
2016-03-26 00:47:20 +01:00