1

This is code that waits for an event coming from button A.

control.waitForEvent(Button.A, EventBusValue.MICROBIT_EVT_ANY)

I want to wait for either button A OR button B to be pressed.

Is this possible?

EDIT

I know that in Scratch this is possible with something like button.any, also the above code is written in microjavascript, but it is written similarly in micropython, so maybe someone from this field can also help. Thank you.

amer
  • 1,528
  • 1
  • 14
  • 22

3 Answers3

0

Have a look at the python code generated using the 'on event from' block in https://makecode.microbit.org. This block can be found under the Advanced tab. The block can be set to wait for either button A or B to be pressed. The micropython code generated in this case is:

control.on_event(EventBusSource.MICROBIT_ID_BUTTON_AB,
    EventBusValue.MICROBIT_EVT_ANY,
    on_microbit_id_button_ab_evt) 

The equivalent JavaScript code can also be viewed in the editor.

Oppy
  • 2,662
  • 16
  • 22
0

The best way I could do it was with common function for A nad B, to be called on events of button pressed, like:

input.onButtonPressed(Button.A, function () {
    qAndA(true)
})
input.onButtonPressed(Button.B, function () {
    qAndA(false)
})

The true and false are not the best implementations but to know which button was pressed, I needed to pass the true for A and false for B.

This was required in my case, because I was writing test and later in this function I would compare the button pressed and the actual correct answer. The function (without my full implementation) goes something like this:

function qAndA(aOrB: boolean) {
    if (text_list.length == 0) {
        basic.showNumber(count)
        basic.pause(2000)
        basic.showLeds(`
            # # # # #
            . . # . .
            . . # . .
            . . # . .
            . . # . .
            `)
    }
    if (text_list[0] == Q1 && aOrB == true) {
        ...
    }
} 
amer
  • 1,528
  • 1
  • 14
  • 22
0

In scratch, placing a [Wait for <Key Pressed (a) ||or|| Key Pressed (b)>], will do the trick, you can just slot a key pressed (a) and a key pressed (b) into the same or statement, and place the new blocks into a "Wait until" block, place this where you want the code to stop and continue on the keypress update and you're golden.