Simple test

Ensure your button works with this simple test.

Button wiring

GND

D9

examples/button_handler_simpletest.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2024 EGJ Moorington
 3#
 4# SPDX-License-Identifier: Unlicense
 5
 6import time
 7
 8import board
 9from keypad import Keys
10
11from button_handler import ButtonHandler
12
13scanner = Keys([board.D9], value_when_pressed=False)
14button_handler = ButtonHandler(scanner.events, set())
15
16while True:
17    inputs = button_handler.update()
18    for input_ in inputs:
19        print(input_)
20    time.sleep(0.01)

Single button

This simple script showcases the usage of this library using a single button.

Button wiring

GND

D9

examples/button_handler_singlebutton.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2024 EGJ Moorington
 3#
 4# SPDX-License-Identifier: Unlicense
 5
 6import time
 7
 8import board
 9from keypad import Keys
10
11from button_handler import ButtonHandler, ButtonInitConfig, ButtonInput
12
13
14def double_press():
15    print("Double press detected!")
16
17
18def short_press():
19    print("Short press detected!")
20
21
22def long_press():
23    print("Long press detected!")
24
25
26def hold():
27    print("The button began being held down!")
28
29
30actions = {
31    ButtonInput(ButtonInput.DOUBLE_PRESS, callback=double_press),
32    ButtonInput(ButtonInput.SHORT_PRESS, callback=short_press),
33    ButtonInput(ButtonInput.LONG_PRESS, callback=long_press),
34    ButtonInput(ButtonInput.HOLD, callback=hold),
35}
36
37scanner = Keys([board.D9], value_when_pressed=False)
38button_handler = ButtonHandler(scanner.events, actions)
39
40
41while True:
42    button_handler.update()
43    time.sleep(0.0025)

Advanced single button

This advanced script demonstrates how to handle triple presses and configure a button.

Button wiring

GND

D9

examples/button_handler_advancedsinglebutton.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2026 EGJ Moorington
 3# SPDX-FileCopyrightText: Copyright (c) 2026 George Hartzell
 4#
 5# SPDX-License-Identifier: Unlicense
 6
 7#
 8# Demonstrates:
 9# - handling a triple press
10# - setting configurations for buttons (e.g. multi_press_interval)
11#
12
13import time
14
15import board
16from keypad import Keys
17
18from button_handler import ButtonHandler, ButtonInitConfig, ButtonInput
19
20
21def double_press():
22    print("Double press detected!")
23
24
25def triple_press():
26    print("Triple press detected!")
27
28
29def short_press():
30    print("Short press detected!")
31
32
33def long_press():
34    print("Long press detected!")
35
36
37def hold():
38    print("The button began being held down!")
39
40
41callback_inputs = {
42    ButtonInput(ButtonInput.DOUBLE_PRESS, 0, double_press),
43    ButtonInput(3, 0, triple_press),
44    ButtonInput(ButtonInput.SHORT_PRESS, 0, short_press),
45    ButtonInput(ButtonInput.LONG_PRESS, 0, long_press),
46    ButtonInput(ButtonInput.HOLD, 0, hold),
47}
48
49
50config = ButtonInitConfig(multi_press_interval=500, max_multi_press=3)
51scanner = Keys((board.D9,), value_when_pressed=False, pull=True)
52button_handler = ButtonHandler(scanner.events, callback_inputs, 1, {0: config})
53
54
55while True:
56    button_handler.update()
57    time.sleep(0.0025)

Double button

This script showcases the usage of this library using two buttons.

Button A wiring

Button B wiring

GND

GND

D9

A2

examples/button_handler_doublebutton.py
  1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
  2# SPDX-FileCopyrightText: Copyright (c) 2024 EGJ Moorington
  3#
  4# SPDX-License-Identifier: Unlicense
  5
  6import time
  7
  8import board
  9from keypad import Keys
 10
 11from button_handler import ButtonHandler, ButtonInitConfig, ButtonInput
 12
 13
 14def double_press_0():
 15    if button_1.is_pressed:
 16        double_press_0_holding_1()
 17        return
 18    print("Button 0 has been double pressed!")
 19
 20
 21def double_press_1():
 22    if button_0.is_pressed:
 23        double_press_1_holding_0()
 24        return
 25    print("Button 1 has been double pressed!")
 26
 27
 28def triple_press_0():
 29    if button_1.is_pressed:
 30        triple_press_0_holding_1()
 31        return
 32    print("Button 0 has been triple pressed!")
 33
 34
 35def triple_press_1():
 36    if button_0.is_pressed:
 37        triple_press_0_holding_1()
 38        return
 39    print("Button 1 has been triple pressed!")
 40
 41
 42def short_press_0():
 43    if button_1.is_pressed:
 44        short_press_0_holding_1()
 45        return
 46    print("Button 0 has been pressed quickly!")
 47
 48
 49def short_press_1():
 50    if button_0.is_pressed:
 51        short_press_1_holding_0()
 52        return
 53    print("Button 1 has been pressed quickly!")
 54
 55
 56def long_press_0():
 57    if button_1.is_pressed:
 58        long_press_0_holding_1()
 59        return
 60    print("Button 0 has been pressed for a long time!")
 61
 62
 63def long_press_1():
 64    if button_0.is_pressed:
 65        long_press_1_holding_0()
 66        return
 67    print("Button 1 has been pressed for a long time!")
 68
 69
 70def double_press_0_holding_1():
 71    print("Button 0 has been double pressed while button 1 was held down!")
 72
 73
 74def double_press_1_holding_0():
 75    print("Button 1 has been double pressed while button 0 was held down!")
 76
 77
 78def triple_press_0_holding_1():
 79    print("Button 0 has been triple pressed while button 1 was held down!")
 80
 81
 82def triple_press_1_holding_0():
 83    print("Button 1 has been triple pressed while button 0 was held down!")
 84
 85
 86def short_press_0_holding_1():
 87    print("Button 0 has been pressed quickly while button 1 was held down!")
 88
 89
 90def short_press_1_holding_0():
 91    print("Button 1 has been pressed quickly while button 0 was held down!")
 92
 93
 94def long_press_0_holding_1():
 95    print("Button 0 has been pressed for a long time while button 1 was held down!")
 96
 97
 98def long_press_1_holding_0():
 99    print("Button 1 has been pressed for a long time while button 0 was held down!")
100
101
102config = ButtonInitConfig(max_multi_press=3)
103scanner = Keys([board.D9, board.A2], value_when_pressed=False)
104callback_inputs = {
105    ButtonInput(ButtonInput.DOUBLE_PRESS, 0, double_press_0),
106    ButtonInput(ButtonInput.DOUBLE_PRESS, 1, double_press_1),
107    ButtonInput(3, 0, triple_press_0),
108    ButtonInput(3, 1, triple_press_1),
109    ButtonInput(ButtonInput.SHORT_PRESS, 0, short_press_0),
110    ButtonInput(ButtonInput.SHORT_PRESS, 1, short_press_1),
111    ButtonInput(ButtonInput.LONG_PRESS, 0, long_press_0),
112    ButtonInput(ButtonInput.LONG_PRESS, 1, long_press_1),
113}
114button_handler = ButtonHandler(scanner.events, callback_inputs, 2, {0: config, 1: config})
115
116button_0 = button_handler.buttons[0]
117button_1 = button_handler.buttons[1]
118
119while True:
120    button_handler.update()
121    time.sleep(0.0025)