822227eb48
* Add brick button topics * Add the rest of the brick api
1.5 KiB
1.5 KiB
on Event
Run some code when a button is clicked, pressed down, or released.
brick.buttonEnter.onEvent(ButtonEvent.Click, function () {
});
~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
.
sensors.touch1.onEvent(TouchSensorEvent.Pressed, function () {
brick.setLight(BrickLight.Orange);
});
Read about touch sensors and using them as touch buttons.
~
Parameters
- ev: the button action to run some code for. The button actions (events) are:
click
: button was clicked (pressed and released)up
: button is released from just being presseddown
: button is just pressed down
- body: the code you want to run when something happens with a button
Example
Check for event on the ENTER button. Put a message on the screen when the button is pressed, clicked, or released.
brick.showString("ENTER is: UP", 1);
brick.buttonEnter.onEvent(ButtonEvent.Up, function () {
brick.showString("ENTER is: UP ", 1);
});
brick.buttonEnter.onEvent(ButtonEvent.Down, function () {
brick.showString("ENTER is: DOWN ", 1);
});
brick.buttonEnter.onEvent(ButtonEvent.Click, function () {
brick.showString("ENTER was: CLICKED", 1);
});