Buttons

This page is about the buttons on the EV3 intelligent brick, not the button in the touch sensor. The official documentation is HERE. Here are some short scripts demonstrating the use of the Button() class.

Wait for bump

The new EV3 Python library (v2) has some great new button functions such as wait_for_bump(buttons). A 'bump' is the press and release of a button. There are also functions wait_for_pressed(buttons) and wait_for_released(buttons) which work in a similar way. Here is a script that demonstrates the use of the new functions. It waits until the left button is bumped, then beeps, then waits until either the up or down button is pressed, then beeps, then waits until the right button is (pressed and) released, then beeps.

As of October 2018 these functions are not working properly - they will be fixed in the next release.

#!/usr/bin/env python3

from ev3dev2.button import Button

from ev3dev2.sound import Sound

btn = Button()

sound = Sound()

btn.wait_for_bump('left')

sound.beep()

btn.wait_for_pressed(['up', 'down'])

sound.beep()

btn.wait_for_released('right')

sound.beep()

Press any button to exit

The loop checks whether any button is pressed and if so beeps and exits the script. The script checks the button state every 0.01 second.

#!/usr/bin/env python3

from ev3dev2.button import Button

from ev3dev2.sound import Sound

from time import sleep

btn = Button()

sound = Sound()

while True:

    if btn.any():    # Checks if any button is pressed.

        sound.beep()  # Wait for the beep to finish.

        exit()  # Stop the program.

    else:

        sleep(0.01)  # Wait 0.01 second

Here's a much neater version of the above script:

#!/usr/bin/env python3

from ev3dev2.button import Button

from ev3dev2.sound import Sound

from time import sleep

btn = Button()

sound = Sound()

while not btn.any(): # While no (not any) button is pressed.

    sleep(0.01)  # Wait 0.01 second

sound.beep()

You may think that these are poor examples because wait_for_bump() could have been used to make a shorter, clearer script but in fact the second script above is a good way of using a button press to exit a loop and perhaps terminate a program.

React to button presses and releases

In this script's loop, the highlighted command btn.process() checks for any change in the state of the buttons. If it detects a change then it triggers the corresponding 'events'. For example, if it detects that the left button has just been pressed then it triggers a 'left button state change' event called on_left and also a 'button change event' called on_change. It also assigns a value of True to the parameter state if the button is pressed and False if the button is released. Event handlers respond to the events. For example, if the left button is pressed then the 'left button state change' event will trigger the highlighted on_left event handler which will call the highlighted function 'left' (the function does not have to have this name).

#!/usr/bin/env python3

from ev3dev2.button import Button

from time import sleep

btn = Button()

# Do something when state of any button changes:

  

def left(state):

    if state:

        print('Left button pressed')

    else:

        print('Left button released')

    

def right(state):  # neater use of 'if' follows:

    print('Right button pressed' if state else 'Right button released')

    

def up(state):

    print('Up button pressed' if state else 'Up button released')

    

def down(state):

    print('Down button pressed' if state else 'Down button released')

    

def enter(state):

    print('Enter button pressed' if state else 'Enter button released')

    

btn.on_left = left

btn.on_right = right

btn.on_up = up

btn.on_down = down

btn.on_enter = enter

# This loop checks button states continuously (every 0.01s). 

# If the new state differs from the old state then the appropriate

# button event handlers are called.

while True:

    btn.process()

    sleep(0.01)

If running this script from VS Code, press the stop button to quit. if running from Brickman, long-press the backspace button to quit.

Obtain a list of changed button states

This script has similar functionality to the previous one but uses the on_change(changed_buttons) event handler to achieve a much more concise script. Not only does it list changed button states but it also reacts to the pressing of a specific button. Press the stop button (VS Code) or long-press the backspace button (Brickman) to end the script.

#!/usr/bin/env python3

from ev3dev2.button import Button

from time import sleep

btn = Button()

def change(changed_buttons):   # changed_buttons is a list of 

# tuples of changed button names and their states.

    print('These buttons changed state: ' + str(changed_buttons))

    if ('left', True) in changed_buttons:

        print('You pressed the left button')

btn.on_change = change

# This loop checks button states

# continuously and calls appropriate event handlers

while True:

    btn.process()

    sleep(0.01)

Check button states

The previous script showed how process() and its corresponding event handlers can be used to detect and respond to CHANGES in state. There are also functions (down, left, enter etc) which simply detect the STATE of the buttons. The state is True if the button is pressed and False otherwise. The script below checks and displays the state of the left button once per second. You can end the script with a one-second press on the Backspace button.

#!/usr/bin/env python3

# prints the state of the left button (True or False) every second

# When left button is pressed the string '^[[D' is also printed

from ev3dev2.button import Button

from time import sleep

btn = Button()

while not btn.backspace:

    print(btn.left)

    sleep(1)

Other button functions

The following script demonstrates two more functions: buttons_pressed and check_buttons(). The function check_buttons()checks whether the currently pressed buttons exactly match the given list and then returns True or False. Every second a list of the pressed buttons is printed and you can end the script by pressing ONLY the left and right buttons simultaneously.

#!/usr/bin/env python3

# Every second a list of the pressed buttons is printed

# and you can end the script by pressing

# ONLY the left and right buttons simultaneously.

from ev3dev2.button import Button

from time import sleep

btn = Button()

while True:

    print(btn.buttons_pressed)

    if btn.check_buttons(buttons=['left','right']):

        exit()

    sleep(1)