This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Double click & long press keyboard

I am interfacing four keys. I am using pull-up resistor for it. So that pins are high when switch is open & low when switch is pressed. I took reference from mazidi & made keypad routine in C & works fine for me. Mazidi gave 4x4 keypad algo & I used it to work for individual switches on four pins. With every press one variable is incremented. With few more tweaks I am able to detect single click & if key is kept pressed then input gets incremented fast. Now I wanted to learn how I can implement difference between single & double click. keys pressed for short time & keys pressed for long time. 1. Any reference algo for it.

  • Embedded World said:
    Now I wanted to learn how I can implement difference between single & double click.

    To detect double-click you would want to measure time between two button press or release events and if time is within predefined time window - then you know it's double-click.

  • Hey,

    Firstly, I donot know the code you were using from Mazidi. cant really suggest you from the perspective of code that had been implemented. That said if I were you, I would do some thing like this.


    Step 1:.
    #Define single_click_duration  500 // key should be held low for atleast 500 timer count
    #define double_click_lapse      300 // time l;apse betwwen two subsequent key presses
    #define Single_long_click_duration 1000//key help for 1000 counts long press

    Step 2:
    Start timer in Counter mode.


    Step 3:
    Setup port interrupts

    Step 4:
    Upon Button Press, start timer and start counting 

    Step5:
    In the timer interrupt , count for 500 (your preferrend timer count) and also monitor Pin state
    ex:
    If ( P2IN & KEY_1) // Button pressed and still holding
    {
    ++timer count ;
    }
    else //released
    {
      if( 1000 > timer_count < 500)  
             {
                   flag_Single_button_press = 1;
              } 
      else if(timer_count >= 100) flag_long_button_press = 1;
    }

    Step 6:

    After flag_single_button_press, monitor another 300 counts for button released and see for subsequent button press which is there for 500 counts again. 

    to consider it as double press.

    Now stop the timer, if the button is released for more than 300, consider as single press and stop the timer.go to the activity where u are performing button press retaed functionality.

    Note: This way you have to wait for atlest 800 timer counts to perform a button related activity. 


**Attention** This is a public forum