pxt-calliope/docs/reference/input/on-button-pressed.md
2016-04-15 15:53:20 -07:00

1.2 KiB

On Button Pressed

Register an event handler that will execute whenever an input button (A, B, or A and B together) is pressed during program execution. When running code with this function in a web browser, click an on-screen input button - labelled A or B.

input.onButtonPressed(Button.A, () => {})

Example: count button clicks

This example counts how many times the left or right input button is pressed. Each time a button is pressed, the global count variable is increased by 1 and displayed on the screen.

let count = 0
basic.showNumber(count)
input.onButtonPressed(Button.A, () => {
    count++;
    basic.showNumber(count);
})

Example: roll a dice

This example generates a random number when you press the B input button, and then displays a random die image:

input.onButtonPressed(Button.B, () => {
    let dice = Math.random(6)
    basic.showNumber(dice)
})

Lessons

smiley, answering machine, screen wipe, rotation animation

See also

button is pressed, forever