<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Detect LongPress-Button (but not only when released button)]]></title><description><![CDATA[<p dir="auto">Hi folks</p>
<p dir="auto">Is there a possibility to detect if a button is long pressed, but not only when the button is released?</p>
<p dir="auto">The implemented function does a long press recognize only if I release the button.<br />
I think no one needs this behavior because it is not usable for an intuitive UI.</p>
<p dir="auto"><code>btnA.pressFor(0.8, buttonA_pressFor)</code></p>
<p dir="auto">Any idea? Or do I have to implement this behavior by myself?<br />
Did I miss something?</p>
<p dir="auto">Otherwise it would be a bit to much code for such a simple thing (especially if we need all three buttons):</p>
<pre><code>from m5stack import *
from m5ui import *
from uiflow import *

setScreenColor(0x222222)

buttonA_LongPressed = 0
buttonB_LongPressed = 0
buttonC_LongPressed = 0
longPress_ms = 800
longPress_interval = 100

def ButtonA_LongPressed():
  rgb.setColorAll(0xff0000)
  wait_ms(100)
  rgb.setColorAll(0x000000)

def ButtonB_LongPressed():
  rgb.setColorAll(0x009900)
  wait_ms(100)
  rgb.setColorAll(0x000000)

def ButtonC_LongPressed():
  rgb.setColorAll(0x3333ff)
  wait_ms(100)
  rgb.setColorAll(0x000000)

@timerSch.event('timer_ButtonLongPressed')
def ttimer_ButtonLongPressed():
  
  global buttonA_LongPressed, buttonB_LongPressed, buttonC_LongPressed, longPress_interval, longPress_ms
  
  if btnA.isPressed():
    buttonA_LongPressed = buttonA_LongPressed + longPress_interval
  else:
    buttonA_LongPressed = 0

  if btnB.isPressed():
    buttonB_LongPressed = buttonB_LongPressed + longPress_interval
  else:
    buttonB_LongPressed = 0

  if btnC.isPressed():
    buttonC_LongPressed = buttonC_LongPressed + longPress_interval
  else:
    buttonC_LongPressed = 0
  
  if buttonA_LongPressed &gt; longPress_ms:
    buttonA_LongPressed = 0
    ButtonA_LongPressed()
  
  if buttonB_LongPressed &gt; longPress_ms:
    buttonB_LongPressed = 0
    ButtonB_LongPressed()
  
  if buttonC_LongPressed &gt; longPress_ms:
    buttonC_LongPressed = 0
    ButtonC_LongPressed()

timerSch.run('timer_ButtonLongPressed', longPress_interval, 0x00)
</code></pre>
<p dir="auto">Best regards<br />
Thomas</p>
]]></description><link>https://community.m5stack.com/topic/1731/detect-longpress-button-but-not-only-when-released-button</link><generator>RSS for Node</generator><lastBuildDate>Wed, 29 Apr 2026 03:37:45 GMT</lastBuildDate><atom:link href="https://community.m5stack.com/topic/1731.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 01 Mar 2020 04:14:35 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Detect LongPress-Button (but not only when released button) on Sun, 15 Mar 2020 11:08:58 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sysdl132" aria-label="Profile: sysdl132">@<bdi>sysdl132</bdi></a> said in <a href="/post/7691">Detect LongPress-Button (but not only when released button)</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/m5stickfreakler" aria-label="Profile: m5stickfreakler">@<bdi>m5stickfreakler</bdi></a> Try to set the longpress time shorter.</p>
</blockquote>
<p dir="auto">Thanks for reply but that wasn't the question. My code in my post works fine. :-)<br />
But it would be great if the implemented Micropython standard solution (with events) didn't just trigger an event when I release the button.<br />
Like my alternative code, most people want that the event is triggered immediately after the "long press time" is past, not only when the button is released.<br />
So the Micropython event solution would be more powerful, if I could solve the "problem" in one code line instead of fifty.<br />
I am not such good in micropython but I think there is unfortunately no simpler solution for that.</p>
]]></description><link>https://community.m5stack.com/post/7755</link><guid isPermaLink="true">https://community.m5stack.com/post/7755</guid><dc:creator><![CDATA[M5StickFreakler]]></dc:creator><pubDate>Sun, 15 Mar 2020 11:08:58 GMT</pubDate></item><item><title><![CDATA[Reply to Detect LongPress-Button (but not only when released button) on Sat, 07 Mar 2020 07:13:53 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/m5stickfreakler" aria-label="Profile: m5stickfreakler">@<bdi>m5stickfreakler</bdi></a> Try to set the longpress time shorter.</p>
<pre><code>from m5stack import *
from m5ui import *
from uiflow import *

setScreenColor(0x222222)

buttonA_LongPressed = 0
buttonB_LongPressed = 0
buttonC_LongPressed = 0
longPress_ms = 600
# or shorter
longPress_interval = 100
#==============================
# Your code here
</code></pre>
]]></description><link>https://community.m5stack.com/post/7691</link><guid isPermaLink="true">https://community.m5stack.com/post/7691</guid><dc:creator><![CDATA[sysdl132]]></dc:creator><pubDate>Sat, 07 Mar 2020 07:13:53 GMT</pubDate></item></channel></rss>