2

Actually, this question extends this one.

I want to count quantity of my pressed button from my headphone.

if(action == KeyEvent.ACTION_DOWN) 
    {
        Toast.makeText(context, "Pressed 1 time", Toast.LENGTH_SHORT).show();
    }
    if(action == KeyEvent.ACTION_MULTIPLE)
    {
        k = event.getRepeatCount();
        if(k == 2)
            Toast.makeText(context, "Pressed 2 times", Toast.LENGTH_SHORT).show();
        if(k == 3)
            Toast.makeText(context, "Pressed 3 times", Toast.LENGTH_SHORT).show();
        if(k>=4)
        {
            String a = Integer.toString(k);
            Toast.makeText(context, "Pressed " + a + " times.", Toast.LENGTH_SHORT).show();
        }
    }

But as I press the button, I get Toast "Pressed 1 time". I always get it, system never steps into other ifs.

How can I solve this?

Thanks

ADDITION Still can't work this out. Here's what I do:

 if(action == KeyEvent.ACTION_DOWN) 
    {
        k++;
        try 
        {
            wait(1000);
        } catch (InterruptedException e) 
        {
            e.printStackTrace();
        }
    }
        if(k == 1)
            Toast.makeText(context, "Pressed 1 time", Toast.LENGTH_SHORT).show();
        if(k == 2)
            Toast.makeText(context, "Pressed 2 times", Toast.LENGTH_SHORT).show();
        if(k == 3)
            Toast.makeText(context, "Pressed 3 times", Toast.LENGTH_SHORT).show();
        if(k>=4)
        {
            String a = Integer.toString(k);
            Toast.makeText(context, "Pressed " + a + " times.", Toast.LENGTH_SHORT).show();
        }
        k = 0;
    abortBroadcast();
Community
  • 1
  • 1
azizbekian
  • 60,783
  • 13
  • 169
  • 249
  • Are you sure that headphones-multiclicking generate "ACTION_MULTIPLE" KeyEvents? – Kheldar Jun 07 '12 at 14:20
  • No, I'm not sure at all. I hope it generates :) I want to control the button. For now, the button click performes PLAY/STOP function for player, I want it to go to next track if I double press, or go to the previous if I triple press. Wonder, how can I do that. – azizbekian Jun 07 '12 at 14:22
  • You can attain this by using a simple counter and increment it in `KeyEvent.ACTION_UP`. – Kazekage Gaara Jun 07 '12 at 14:23
  • I believe, you should log to console to be sure what happens, and run a time based counter to achieve your goal if ACTION_MULTIPLE doesn't work --as in, get time of first click, up counter if first click still in given timeframe (1s, 2s, or likewise) – Kheldar Jun 07 '12 at 14:24
  • So, in that case, I have to switch timer and control the next click, if got, create another timer, and so on...But it becomes too complicated. I hope there is another way. If haven't got answer, I have no way, will do that in this way. – azizbekian Jun 07 '12 at 14:27
  • No, check Antal's explanation, you just need one timeframe (as a "floating window" that starts on your firstclick) since obviously two clicks separated by 20 seconds aren't a multiple-click. – Kheldar Jun 07 '12 at 14:29
  • @KazekageGaara, everytime I get ACTION_DOWN, I should reset the timer. Am I right? – azizbekian Jun 07 '12 at 14:29
  • @andranikAzizbekyan You should probably go the way Antal has suggested. Run a thread and call a wait for about 500 or say 1000 ms and reset your counter after that. All clicks made within that time period should suffice for your need. – Kazekage Gaara Jun 07 '12 at 14:31
  • It depends on if you want to be able to detect double/triple clicks or if you want to be able to detect 200000-clicks. Are you planning on creating a earplug-controlled machine-gun? If not, probably you shouldn't :D – Kheldar Jun 07 '12 at 14:31

1 Answers1

3

"multiple duplicate key events have occurred in a row" is the definition of ACTION_MULTIPLE, you are only pressing one button.

if(action == KeyEvent.ACTION_DOWN) 
{
    k++;
    Toast.makeText(context,  "Pressed " + Integer.toString(k) + " time(s).", Toast.LENGTH_SHORT).show();
}

Edit:

Oh i see you want to next track on double or back on triple clicks. I suggest implementing a thread. In your KeyEvent.ACTION_DOWN count the key presses. In your thread, let's say call a wait(500) and check how many times your button have been pressed and act accordingly and set back the counter to 0.