Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

android - How to differentiate between long key press and regular key press?

I'm trying to override the functionality of the back key press. When the user presses it once, I want it to come back to the previous screen. However, when the back key is long-pressed (for, let's say, two seconds or more), I want to exit the application.

By now, I have overriden these two methods in my activity:

@Override
public boolean onKeyDown( int keyCode, KeyEvent event){
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        //manage short keypress
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyLongPress( int keyCode, KeyEvent event){
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        //manage long keypress (different code than short one)
        return true;
    }
    return super.onKeyLongPress(keyCode, event);
}

But the onKeyLongPress callback is never called, because the event is always received by the onKeyDown method.

Is there any way of having both methods working? Or has it to be done all in the onKeyDown and use the number of repetitions/milliseconds to detect it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The reason why onKeyLongPress is never called, is that you return true in onKeyDown without telling the framework that this might be a long press - causing the KeyEvent to stop its flow through the different event handlers.

What you need to do is this:

  1. Before you return true, call event.startTracking() as explained in the documentation.
  2. Handle the long press in onKeyLongPress.

Implement as below and it will work:

  @Override
  public boolean onKeyDown( int keyCode, KeyEvent event ) {
    if( keyCode == KeyEvent.KEYCODE_BACK ) {
      event.startTracking();
      return true; 
    }
    return super.onKeyDown( keyCode, event );
  }

  @Override
  public boolean onKeyUp( int keyCode, KeyEvent event ) {
    if( keyCode == KeyEvent.KEYCODE_BACK ) {
      //Handle what you want on short press.      
      return true; 
    }

    return super.onKeyUp( keyCode, event );
  }

  @Override
  public boolean onKeyLongPress( int keyCode, KeyEvent event ) {
    if( keyCode == KeyEvent.KEYCODE_BACK ) {
      //Handle what you want in long press.
      return true;
    }
    return super.onKeyLongPress( keyCode, event );
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...