MajicDesigns / MD_UISwitch

Uniformly encapsulate different types of switches as user input devices
GNU Lesser General Public License v2.1
40 stars 17 forks source link

Detect multiple concurrent button presses or long presses. #15

Closed pbinsted closed 2 years ago

pbinsted commented 2 years ago

I'm really digging this library, great work.

I am trying to detect when two buttons are held simultaneously for a period of time (longpress).

I've tried a basic IF statement using both switches KEY_DOWN during the loop but have had no luck.

Thanks, Pete

MajicDesigns commented 2 years ago

With no code it is hard to say what you are doing wrong.

  1. Make sure the switches are separate objects as you need the detection to work concurrently.
  2. The status is only sent once when the status is detected. It is unlikely that the detection will be at the same time for multiple switches, so a simple if (switch1==keydown and switch2 == keydown) is highly unlikely to work. You should store the last non-null status of each switch and compare the two stored statuses. Issues then arise of how close they need to be for it to register as 'simultaneous' and that needs to be handled by a timer in the application.
pbinsted commented 2 years ago

Hi. I think you nailed it with point 2. That is what I figured was happening. Thanks so much for the help and the great libraries.

MajicDesigns commented 2 years ago

FWIW I added a new 'Concurrent' example in the latest version (v2.2.1) to test that my idea was correct.

pbinsted commented 2 years ago

Just wanted to say thanks again. Your concurrent example really helped me and I got my code working right away. Application is for a lap timer for a custom slot car track I built. I really appreciate your help and your libraries.