I want my application to react differently to a normal and a long pressed Key Event of the Volume Buttons.
I’ve already seen this, but if I keep the volume button pressed, I get a lot of KeyDown Events before I get the KeyLongPressed Event.
I’d like to have either one event or the other, not both, so that I can adjust the Volume at a short press and skip a track at a long press.
Can you help me out here?
This is my code:
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)
{
Log.d("Test", "Long press!");
return true;
}
return super.onKeyLongPress(keyCode, event);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
event.startTracking();
Log.d("Test", "Short");
return true;
}
return super.onKeyDown(keyCode, event);
}
Any help appreciated!
– Iris
Here is the code that I wrote. It works like a charm. May be you can optimize it for better logic. But you will get the point with it. The key is to use flags. Short press is a press where we press volume button for short time and release. So onKeyUp is the one which will help us detect short presses.
Logcat for all long presses(No short press detected):
Logcat for all short presses: