🤖Have you ever tried Chat.M5Stack.com before asking??😎
    M5Stack Community
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Register
    • Login

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

    General
    2
    3
    6.5k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • M5StickFreaklerM
      M5StickFreakler
      last edited by M5StickFreakler

      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

      sysdl132S 1 Reply Last reply Reply Quote 0
      • sysdl132S
        sysdl132 @M5StickFreakler
        last edited by

        @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
        

        Baudrate for M5 core/Stick/camera:115200/921600
        For M5StickC:115200/230400/750000/1500000
        For M5StickV:115200/921600/1500000
        For Atom:115200/230400/921600

        M5StickFreaklerM 1 Reply Last reply Reply Quote 0
        • M5StickFreaklerM
          M5StickFreakler @sysdl132
          last edited by

          @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.

          1 Reply Last reply Reply Quote 0
          • First post
            Last post