|
1 | 1 | #include <Arduino.h>
|
2 | 2 | #include <Keyboard.h>
|
3 |
| -#include <ezButton.h> |
4 |
| -#include <elapsedMillis.h> |
| 3 | +#include <AceButton.h> |
| 4 | +using namespace ace_button; |
5 | 5 |
|
6 |
| -ezButton button(11); |
7 |
| -bool isPressed = false; |
8 |
| -elapsedMillis t; |
| 6 | +const int PIN_BUTTON = 11; |
| 7 | + |
| 8 | +AceButton button(PIN_BUTTON); |
| 9 | + |
| 10 | +void handleEvent(AceButton *, uint8_t, uint8_t); |
9 | 11 |
|
10 | 12 | void setup()
|
11 | 13 | {
|
12 |
| - button.setDebounceTime(50); |
| 14 | + ButtonConfig *buttonConfig = ButtonConfig::getSystemButtonConfig(); |
| 15 | + buttonConfig->setEventHandler(handleEvent); |
| 16 | + buttonConfig->setFeature(ButtonConfig::kFeatureLongPress); |
| 17 | + buttonConfig->setLongPressDelay(300); |
| 18 | + |
| 19 | + pinMode(PIN_BUTTON, INPUT_PULLUP); |
13 | 20 | }
|
14 | 21 |
|
15 | 22 | void loop()
|
16 | 23 | {
|
17 |
| - button.loop(); |
| 24 | + button.check(); |
| 25 | +} |
18 | 26 |
|
19 |
| - if (button.isPressed()) |
| 27 | +void handleEvent(AceButton *button, uint8_t eventType, uint8_t buttonState) |
| 28 | +{ |
| 29 | + if (eventType == AceButton::kEventPressed) |
20 | 30 | {
|
21 |
| - if (!isPressed) |
22 |
| - { |
23 |
| - isPressed = true; |
24 |
| - t = 0; |
25 |
| - } |
| 31 | + Keyboard.press(KEY_LEFT_CTRL); |
| 32 | + Keyboard.press(KEY_RIGHT_ARROW); |
| 33 | + delay(10); |
| 34 | + Keyboard.releaseAll(); |
26 | 35 | }
|
27 |
| - |
28 |
| - if (isPressed && t >= 300) |
| 36 | + if (eventType == AceButton::kEventLongPressed) |
29 | 37 | {
|
30 |
| - if (button.getCount() == 1) |
31 |
| - { |
32 |
| - Keyboard.press(KEY_LEFT_CTRL); |
33 |
| - Keyboard.press(KEY_RIGHT_ARROW); |
34 |
| - } |
35 |
| - if (button.getCount() > 1) |
36 |
| - { |
37 |
| - Keyboard.press(KEY_TAB); |
38 |
| - } |
39 |
| - |
| 38 | + Keyboard.press(KEY_TAB); |
40 | 39 | delay(10);
|
41 | 40 | Keyboard.releaseAll();
|
42 |
| - button.resetCount(); |
43 |
| - isPressed = false; |
44 | 41 | }
|
| 42 | + Serial.print("Button event: "); |
45 | 43 | }
|
0 commit comments