Once a button is on screen you need two things: a way to determine whether a touch coordinate landed inside the button, and a way to track when the button transitions between the pressed and released states.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/Marcussacapuces91/doc-TFT_eSPI/llms.txt
Use this file to discover all available pages before exploring further.
TFT_eSPI_Button bundles both capabilities together. The contains() method performs a simple bounding-rectangle hit test. The press() / isPressed() / justPressed() / justReleased() group maintains a two-frame state machine — current state and previous state — so you can detect the exact frame on which the button was first touched or first released, without any external debounce flags.
contains()
Returns true when the point (x, y) lies inside the button’s bounding rectangle.
Horizontal coordinate of the point to test, in screen pixels — typically the x value returned by
tft.getTouch().Vertical coordinate of the point to test, in screen pixels.
bool — true if both x and y fall within the button rectangle; false otherwise.
press()
Feeds the current touch state into the button’s internal state machine. Call this every iteration of loop() with true when the button is being touched and false when it is not. Internally, the previous state is shifted into a history slot before the new state is recorded, enabling justPressed() and justReleased() to compare the two frames.
Pass
true if the button is currently being touched; false if it is not. Combine with contains() to derive this value: btn.press(btn.contains(tx, ty)).isPressed()
Returns true if the button was marked as pressed in the most recent call to press().
justPressed()
Returns true only on the first loop iteration after the button transitions from released → pressed — i.e. the previous state was false and the current state is true.
justReleased()
Returns true only on the first loop iteration after the button transitions from pressed → released — i.e. the previous state was true and the current state is false.
State-machine pattern
The recommended pattern for every loop tick is:- Read the touch controller.
- Call
press()with the result ofcontains()(orfalsewhen there is no touch at all). - Check
justPressed()to fire one-shot actions on touch-down. - Check
justReleased()to fire one-shot actions on lift-off. - Optionally check
isPressed()for continuous held-down behaviour.
Always call
press() once per loop iteration, even when there is no touch. Passing false is how the state machine learns the button has been released and arms justReleased() for the next frame.