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
6"""
7This simple test can be used to ensure the hardware is working properly.
8"""
9
10import time
11
12import board
13from keypad import Keys
14
15from button_handler import ButtonHandler
16
17scanner = Keys([board.D9], value_when_pressed=False)
18button_handler = ButtonHandler(scanner.events, set())
19
20while True:
21 inputs = button_handler.update()
22 for input_ in inputs:
23 print(input_)
24 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_single_button.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
6"""
7This example demonstrates simple usage of the library for a single button set-up.
8"""
9
10import time
11
12import board
13from keypad import Keys
14
15from button_handler import ButtonHandler, ButtonInput
16
17
18def double_press():
19 print("Double press detected!")
20
21
22def short_press():
23 print("Short press detected!")
24
25
26def long_press():
27 print("Long press detected!")
28
29
30def hold():
31 print("The button began being held down!")
32
33
34actions = {
35 ButtonInput(ButtonInput.DOUBLE_PRESS, callback=double_press),
36 ButtonInput(ButtonInput.SHORT_PRESS, callback=short_press),
37 ButtonInput(ButtonInput.LONG_PRESS, callback=long_press),
38 ButtonInput(ButtonInput.HOLD, callback=hold),
39}
40
41scanner = Keys([board.D9], value_when_pressed=False)
42button_handler = ButtonHandler(scanner.events, actions)
43
44
45while True:
46 button_handler.update()
47 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_advanced_single_button.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"""
8This example demonstrates advanced usage of the library for a single button set-up.
9
10The example shows how to:
11- Handle a triple press
12- Set configurations for buttons (e.g. multi_press_interval)
13"""
14
15import time
16
17import board
18from keypad import Keys
19
20from button_handler import ButtonHandler, ButtonInitConfig, ButtonInput
21
22
23def double_press():
24 print("Double press detected!")
25
26
27def triple_press():
28 print("Triple press detected!")
29
30
31def short_press():
32 print("Short press detected!")
33
34
35def long_press():
36 print("Long press detected!")
37
38
39def hold():
40 print("The button began being held down!")
41
42
43callback_inputs = {
44 ButtonInput(ButtonInput.DOUBLE_PRESS, 0, double_press),
45 ButtonInput(3, 0, triple_press),
46 ButtonInput(ButtonInput.SHORT_PRESS, 0, short_press),
47 ButtonInput(ButtonInput.LONG_PRESS, 0, long_press),
48 ButtonInput(ButtonInput.HOLD, 0, hold),
49}
50
51
52config = ButtonInitConfig(multi_press_interval=500, max_multi_press=3)
53scanner = Keys((board.D9,), value_when_pressed=False, pull=True)
54button_handler = ButtonHandler(scanner.events, callback_inputs, 1, {0: config})
55
56
57while True:
58 button_handler.update()
59 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_double_button.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
6"""
7This example demonstrates simple usage of the library for a two button set-up.
8"""
9
10import time
11
12import board
13from keypad import Keys
14
15from button_handler import ButtonHandler, ButtonInput
16
17
18def double_press_0():
19 print("Button 0 has been double pressed!")
20
21
22def double_press_1():
23 print("Button 1 has been double pressed!")
24
25
26def short_press_0():
27 print("Button 0 has been pressed quickly!")
28
29
30def short_press_1():
31 print("Button 1 has been pressed quickly!")
32
33
34def long_press_0():
35 print("Button 0 has been pressed for a long time!")
36
37
38def long_press_1():
39 print("Button 1 has been pressed for a long time!")
40
41
42scanner = Keys([board.D9, board.A2], value_when_pressed=False)
43callback_inputs = {
44 ButtonInput(ButtonInput.DOUBLE_PRESS, 0, double_press_0),
45 ButtonInput(ButtonInput.DOUBLE_PRESS, 1, double_press_1),
46 ButtonInput(ButtonInput.SHORT_PRESS, 0, short_press_0),
47 ButtonInput(ButtonInput.SHORT_PRESS, 1, short_press_1),
48 ButtonInput(ButtonInput.LONG_PRESS, 0, long_press_0),
49 ButtonInput(ButtonInput.LONG_PRESS, 1, long_press_1),
50}
51
52button_handler = ButtonHandler(scanner.events, callback_inputs, 2)
53
54while True:
55 button_handler.update()
56 time.sleep(0.0025)
Advanced double button
This advanced script demonstrates how to handle triple presses, configure buttons and handle button presses while another button is being held down.
Button A wiring |
Button B wiring |
|---|---|
GND |
GND |
D9 |
A2 |
examples/button_handler_advanced_double_button.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
6"""
7This example demonstrates advanced usage of the library for a two button set-up.
8
9The example shows how to:
10- Handle triple presses
11- Handle a button press while another is being held down
12- Set configurations for buttons (e.g. multi_press_interval)
13"""
14
15import time
16
17import board
18from keypad import Keys
19
20from button_handler import ButtonHandler, ButtonInitConfig, ButtonInput
21
22
23def double_press_0():
24 if button_1.is_pressed:
25 double_press_0_holding_1()
26 return
27 print("Button 0 has been double pressed!")
28
29
30def double_press_1():
31 if button_0.is_pressed:
32 double_press_1_holding_0()
33 return
34 print("Button 1 has been double pressed!")
35
36
37def triple_press_0():
38 if button_1.is_pressed:
39 triple_press_0_holding_1()
40 return
41 print("Button 0 has been triple pressed!")
42
43
44def triple_press_1():
45 if button_0.is_pressed:
46 triple_press_0_holding_1()
47 return
48 print("Button 1 has been triple pressed!")
49
50
51def short_press_0():
52 if button_1.is_pressed:
53 short_press_0_holding_1()
54 return
55 print("Button 0 has been pressed quickly!")
56
57
58def short_press_1():
59 if button_0.is_pressed:
60 short_press_1_holding_0()
61 return
62 print("Button 1 has been pressed quickly!")
63
64
65def long_press_0():
66 if button_1.is_pressed:
67 long_press_0_holding_1()
68 return
69 print("Button 0 has been pressed for a long time!")
70
71
72def long_press_1():
73 if button_0.is_pressed:
74 long_press_1_holding_0()
75 return
76 print("Button 1 has been pressed for a long time!")
77
78
79def double_press_0_holding_1():
80 print("Button 0 has been double pressed while button 1 was held down!")
81
82
83def double_press_1_holding_0():
84 print("Button 1 has been double pressed while button 0 was held down!")
85
86
87def triple_press_0_holding_1():
88 print("Button 0 has been triple pressed while button 1 was held down!")
89
90
91def triple_press_1_holding_0():
92 print("Button 1 has been triple pressed while button 0 was held down!")
93
94
95def short_press_0_holding_1():
96 print("Button 0 has been pressed quickly while button 1 was held down!")
97
98
99def short_press_1_holding_0():
100 print("Button 1 has been pressed quickly while button 0 was held down!")
101
102
103def long_press_0_holding_1():
104 print("Button 0 has been pressed for a long time while button 1 was held down!")
105
106
107def long_press_1_holding_0():
108 print("Button 1 has been pressed for a long time while button 0 was held down!")
109
110
111config = ButtonInitConfig(max_multi_press=3)
112scanner = Keys([board.D9, board.A2], value_when_pressed=False)
113callback_inputs = {
114 ButtonInput(ButtonInput.DOUBLE_PRESS, 0, double_press_0),
115 ButtonInput(ButtonInput.DOUBLE_PRESS, 1, double_press_1),
116 ButtonInput(3, 0, triple_press_0),
117 ButtonInput(3, 1, triple_press_1),
118 ButtonInput(ButtonInput.SHORT_PRESS, 0, short_press_0),
119 ButtonInput(ButtonInput.SHORT_PRESS, 1, short_press_1),
120 ButtonInput(ButtonInput.LONG_PRESS, 0, long_press_0),
121 ButtonInput(ButtonInput.LONG_PRESS, 1, long_press_1),
122}
123button_handler = ButtonHandler(scanner.events, callback_inputs, 2, {0: config, 1: config})
124
125button_0 = button_handler.buttons[0]
126button_1 = button_handler.buttons[1]
127
128while True:
129 button_handler.update()
130 time.sleep(0.0025)