Skip to content

Repository files navigation

RepeatButton

RepeatButton represents a control that raises its press event repeatedly from the time it is pressed until it is released. The repeatDelay property determines when the event begins. You can also control the repeat rate of the repetitions with the repeat rate property.

Syntax

Declaration

RepeatButtonrepeatButtonVariable;

Button Events

enumButtonEvents {
PRESS=1, // Occurs when a key is pressedRELEASE=2, // Occurs when a key is releasedHOLD=3, // Occurs when a key is holdREPEAT=4// Occurs when a key is pressed an hold
};

Execute

boolrepeatButton(); // Executing the Repeat button function

Parameters

voidbuttonMode(uint8_tpin, uint8_tmode/*INPUT | INPUT_PULLUP*/);
voiddebounceDelay(uint16_tdebounceDelay/*mS*/);
voidrepeatDelay(uint16_trepeatDelay/*mS*/, uint16_trepeatRate/*mS*/);

Events

voidbuttonEvents(KeyEventCallbackkeyEventCallback); // Occurs when a key is changed// typedef void (*KeyEventCallback) (ButtonEvents e);

Returns

boolisKeyPressed(); // Determines whether the specified key is pressedboolisKeyReleased(); // Determines whether the specified key is releaseboolisKeyHolding(); // Determines whether the specified key is holdingboolisRepeating(); // Determines whether the specified key is repeating

Examples

Key Press and onKeyPressed

#include"RepeatButton.h"#definebuttonPin 2 // Define the button input pin.
RepeatButtonbutton; // Decalre the RepeatButton objectvoidOnKeyStateChanged(ButtonEventse); // Occurs when a key is changedvoidsetup() {
Serial.begin(115200);
button.buttonMode(buttonPin, INPUT_PULLUP); // Set the button modebutton.buttonEvents(OnKeyStateChanged); // Configure the callback function event on the key pressedpinMode(LED_BUILTIN, OUTPUT); // Set the LED_BUILTIN mode
}
voidloop() {
button.repeatButton(); // Executing the Repeat button functionif (button.isKeyReleased(true)) { // Steady State Key Pressed -> False = Rising Edge, True = Seady StatedigitalWrite(LED_BUILTIN, HIGH); // Turned LED ON
} else {
digitalWrite(LED_BUILTIN, LOW); // Turned LED OFF
}
}
voidOnKeyStateChanged(ButtonEventse) {
Serial.println("Event on Key Pressed"); // Print message event on key Pressed
}

Key Release and onKeyReleased

#include"RepeatButton.h"#definebuttonPin 2 // Define the button input pin.
RepeatButtonbutton; // Decalre the RepeatButton objectvoidOnKeyStateChanged(ButtonEventse); // Occurs when a key is changedvoidsetup() {
Serial.begin(115200);
button.buttonMode(buttonPin, INPUT_PULLUP); // Set the button modebutton.buttonEvents(OnKeyStateChanged); // Configure the callback function event on the key releasedpinMode(LED_BUILTIN, OUTPUT); // Set the LED_BUILTIN mode
}
voidloop() {
button.repeatButton(); // Executing the Repeat button functionif (button.isKeyReleased(true)) { // Steady State Key Released -> False = Rising Edge, True = Seady StatedigitalWrite(LED_BUILTIN, HIGH); // Turned LED ON
} else {
digitalWrite(LED_BUILTIN, LOW); // Turned LED OFF
}
}
voidOnKeyStateChanged(ButtonEventse) {
Serial.println("Event on Key Released"); // Print message event on key Released
}

Key Hold and onKeyHolding

#include"RepeatButton.h"#definebuttonPin 2 // Define the button input pin.
constuint16_tholdDelay=3000; // Set Hold Time 3000 millisecondsRepeatButtonbutton; // Decalre the RepeatButton objectvoidOnKeyStateChanged(ButtonEventse); // Occurs when a key is changedvoidsetup() {
Serial.begin(115200);
button.buttonMode(buttonPin, INPUT_PULLUP); // Set the button modebutton.holdDelay(holdDelay); // Set the Hold delaybutton.buttonEvents(OnKeyStateChanged); // Configure the callback function event on the key holdingpinMode(LED_BUILTIN, OUTPUT); // Set the LED_BUILTIN mode
}
voidloop() {
button.repeatButton(); // Executing the Repeat button functionif (button.isKeyHolding(true)) { // Steady State Key Holding -> False = Rising Edge, True = Seady StatedigitalWrite(LED_BUILTIN, HIGH); // Turned LED ON
} else {
digitalWrite(LED_BUILTIN, LOW); // Turned LED OFF
}
}
voidOnKeyStateChanged(ButtonEventse) {
Serial.println("Event on Key Hold"); // Print message event on key Hold
}

Key Repeat and onKeyRepeating

#include"RepeatButton.h"#definebuttonPin 2 // Define the button input pin.
constuint16_trepeatDelay=500; // Set Delay 500 millisecondsconstuint16_trepeatRate=200; // Set Repeat rate 200 millisecondsRepeatButtonbutton; // Decalre the RepeatButton objectvoidOnKeyStateChanged(ButtonEventse); // Occurs when a key is changedvoidsetup() {
Serial.begin(115200);
button.buttonMode(buttonPin, INPUT_PULLUP); // Set the button modebutton.repeatDelay(repeatDelay, repeatRate); // Set the Hold delaybutton.buttonEvents(OnKeyStateChanged); // Configure the callback function event on the key holdingpinMode(LED_BUILTIN, OUTPUT); // Set the LED_BUILTIN mode
}
voidloop() {
button.repeatButton(); // Executing the Repeat button functionif (button.isRepeating()) { // Steady State Key Repeating -> False = Rising Edge, True = Seady StatedigitalWrite(LED_BUILTIN, HIGH); // Turned LED ON
} else {
digitalWrite(LED_BUILTIN, LOW); // Turned LED OFF
}
}
voidOnKeyStateChanged(ButtonEventse) {
switch (e) {
casePRESS:
Serial.println("Event on Key Pressed"); // Print message event on key pressedbreak;
caseREPEATSerial.println("Event on Key Repeat"); // Print message event on key repeat break;
}
}

Releases

Packages

Used by

Contributors

Languages