Learn EV3 Python‎ > ‎

Buttons

The official documentation is HERE.

Here are some short scripts demonstrating the use of the Button() class.
  • If you run the scripts from the command line then the Brickman interface will continue to be displayed and to respond while the script is running. The print command will print to the terminal. A script can be forced to end with Ctrl-C.
  • If you run the scripts from Brickman's file browser then the EV3's screen will be cleared so that the script can print to it. The Brickman interface will reappear when the script ends. The script can be forced to end by long-pressing the Backspace button.
Currently, for unknown reasons, these scripts work for me only from the command line - they close immediately if run from Brickman.

Press any button to exit
The loop checks whether any button is pressed and if so exits the script. The last two lines make the script check the button state every 0.01 second but they are not really necessary.

#!/usr/bin/env python3

from ev3dev.ev3 import *
from time import sleep

btn = Button()

while True:
    if btn.any():    # Checks if any button is pressed.
        exit()
    else:
        sleep(0.01)  # Check for button press every 0.01 second

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 and a 'button change' event. It also assigns a value of True to the parameter state if the button is pressed and a value of 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 function 'left' (the function does not have to have this name).

#!/usr/bin/env python3

from ev3dev.ev3 import *
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')
    
def backspace(state):
    print('Backspace button pressed' if state else 'Backspace button released')
    
btn.on_left = left
btn.on_right = right
btn.on_up = up
btn.on_down = down
btn.on_enter = enter
btn.on_backspace = backspace

while True:  # This loop checks buttons state continuously, 
             # calls appropriate event handlers
    btn.process() # Check for currently pressed buttons. 
    # If the new state differs from the old state, 
    # call the appropriate button event handlers.
    sleep(0.01)  # buttons state will be checked every 0.01 second

# If running this script via SSH, press Ctrl+C to quit
# if running this script from Brickman, long-press 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. Press Ctrl-C (command line) or long-press the backspace button (Brickman) to end the script.

#!/usr/bin/env python3

from ev3dev.ev3 import *
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))

btn.on_change = change

while True:  # This loop checks buttons state
# continuously and calls appropriate event handlers
    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 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

from ev3dev.ev3 import *
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(). 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

from ev3dev.ev3 import *
from time import sleep

btn = Button()

while True:
    print(btn.buttons_pressed)
    if btn.check_buttons(buttons=['left','right']):
        exit()
    sleep(1)
Comments