Mister Smith
Mister Smith

Reputation: 28158

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?

Upvotes: 11

Views: 18115

Answers (4)

In my opinion when you override onkeypress and onkeylongpress. It will automatically call onkeypress everytime you press the key for once or for long. To avoid this you have to write event.starttracking() in onkeypress, but it will result only in functionality of long key press, key press would not work in this scenerio.

For this soloution. You should override onkeydown and onkeyup. make a bool and work with it. Heres my code:

var isKeyDown: Boolean = false

override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
    return when (keyCode) {
        KeyEvent.KEYCODE_VOLUME_DOWN -> {
            isKeyDown = false
            true
        }
        KeyEvent.KEYCODE_VOLUME_UP -> {
            isKeyDown = false
            true
        }

        else -> super.onKeyUp(keyCode, event)
    }
}

/** When key down event is triggered, relay it via local broadcast so fragments can handle it */
override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
    return when (keyCode) {
        KeyEvent.KEYCODE_VOLUME_DOWN -> {
            if (!isKeyDown) {
                val intent =
                    Intent(KEY_EVENT_ACTION).apply { putExtra(KEY_EVENT_EXTRA, keyCode) }
                LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
            }
            isKeyDown = true
            true
        }
        KeyEvent.KEYCODE_VOLUME_UP -> {
            if (!isKeyDown) {
                val intent =
                    Intent(KEY_EVENT_ACTION).apply { putExtra(KEY_EVENT_EXTRA, keyCode) }
                LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
            }
            isKeyDown = true
            true
        }

        else -> super.onKeyDown(keyCode, event)
    }
}

Upvotes: 0

android developer
android developer

Reputation: 116322

I think the best way to handle it is as such.

The only downside I can see here, is that single clicking the menu button doesn't make a sound in case the user has it enabled. Maybe there is a way to check this setting and use it, or call the default behavior.

Code:

private boolean _handledMenuButton=false;

@Override
public boolean onKeyUp(final int keyCode,final KeyEvent event) {
    switch(keyCode) {
        case KeyEvent.KEYCODE_MENU:
          if (!_handledMenuButton) {
              //Handle single clicking here
          }
          _handledMenuButton=false;
          return true;
    }
    return super.onKeyUp(keyCode,event);
}

@Override
public boolean onKeyLongPress(final int keyCode, final KeyEvent event) {
    switch(keyCode) {
        case KeyEvent.KEYCODE_MENU:
          //Handle long clicking here
          _handledMenuButton=true;
          return true;
    }
    return super.onKeyLongPress(keyCode,event);
}

@Override
public boolean onKeyDown(final int keyCode,final KeyEvent event) {
    switch(keyCode) {
        case KeyEvent.KEYCODE_MENU:
            _handledMenuButton=false;
            event.startTracking();
            return true;
    }
    return super.onKeyDown(keyCode,event);
}

Upvotes: 2

kaspermoerch
kaspermoerch

Reputation: 16570

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 );
  }

Upvotes: 18

Mark Allison
Mark Allison

Reputation: 21909

Why not use onKeyUp() as well as onKeyDown()? During onKeyDown() you do not know whether it is a long press or not, because that is called as soon as the key is pressed and you do not know how long the user intends to hold the key down for. As KasperMoerch correctly says, you need to call startTracking in your onKeyDown() method, and return true. Then in your onKeyUp() you can call event.isTracking() and event.isLongPress() to determine whether to handle things as a long press or short press.

Upvotes: 2

Related Questions