Detect LongPress-Button (but not only when released button)



  • Hi folks

    Is there a possibility to detect if a button is long pressed, but not only when the button is released?

    The implemented function does a long press recognize only if I release the button.
    I think no one needs this behavior because it is not usable for an intuitive UI.

    btnA.pressFor(0.8, buttonA_pressFor)

    Any idea? Or do I have to implement this behavior by myself?
    Did I miss something?

    Otherwise it would be a bit to much code for such a simple thing (especially if we need all three buttons):

    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 > longPress_ms:
        buttonA_LongPressed = 0
        ButtonA_LongPressed()
      
      if buttonB_LongPressed > longPress_ms:
        buttonB_LongPressed = 0
        ButtonB_LongPressed()
      
      if buttonC_LongPressed > longPress_ms:
        buttonC_LongPressed = 0
        ButtonC_LongPressed()
    
    timerSch.run('timer_ButtonLongPressed', longPress_interval, 0x00)
    

    Best regards
    Thomas



  • @m5stickfreakler Try to set the longpress time shorter.

    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
    


  • @sysdl132 said in Detect LongPress-Button (but not only when released button):

    @m5stickfreakler Try to set the longpress time shorter.

    Thanks for reply but that wasn't the question. My code in my post works fine. :-)
    But it would be great if the implemented Micropython standard solution (with events) didn't just trigger an event when I release the button.
    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.
    So the Micropython event solution would be more powerful, if I could solve the "problem" in one code line instead of fifty.
    I am not such good in micropython but I think there is unfortunately no simpler solution for that.