Micro:bit参考

Micro:bit参考 > Micropython API > Buttons(按钮)

Buttons 按钮

There are two buttons on the board, called button_a and button_b.
板上有两个按钮,分别称为 button_a 和 button_b。

Attributes  属性 

button_a

A Button instance (see below) representing the left button.
一个 Button 实例(见下文),表示左按钮。

button_b

Represents the right button.
表示右按钮。

Classes  类 

classButton

Represents a button.  表示按钮。

Note  注意

This class is not actually available to the user, it is only used by the two button instances, which are provided already initialized.
这个类实际上对用户不可用,它只由两个 button 实例使用,这两个实例已经初始化。

is_pressed()

Returns True if the specified button button is currently being held down, and False otherwise.
如果当前正在按住指定的按钮按钮 ,则返回 True,否则返回 False。

was_pressed()

Returns True or False to indicate if the button was pressed (went from up to down) since the device started or the last time this method was called. Calling this method will clear the press state so that the button must be pressed again before this method will return True again.
返回 True 或 False 以指示是否按下了按钮 (从上到下)自设备启动或上次执行此作 方法。 调用该方法将清除 press 状态,因此 在此方法返回之前,必须再次按下该按钮 又一次正确 。

get_presses()

Returns the running total of button presses, and resets this total to zero before returning.
返回按钮按下的运行总数,并在返回之前将此总数重置为零。

Example  示例 

import microbit

while True:
    if microbit.button_a.is_pressed() and microbit.button_b.is_pressed():
        microbit.display.scroll("AB")
        break
    elif microbit.button_a.is_pressed():
        microbit.display.scroll("A")
    elif microbit.button_b.is_pressed():
        microbit.display.scroll("B")
    microbit.sleep(100)