1,key -- 实体按键,
现在手机物理按键越来越少
常见的有 KEYCODE_VOLUME_DOWN/UP KEYCODE_POWER KEYCODE_BACK KEYCODE_HOME KEYCODE_MENU
在一个activity 重载父类 的下面这三个方法来处理按键事件
public boolean onKeyDown(int keyCode, KeyEvent event)
public boolean onKeyUp(int keyCode, KeyEvent event)
public boolean onKeyLongPress(int keyCode, KeyEvent event)
很明显,长按事件一般是放到 onKeyLongPress 函数中来处理的。
那具体是怎么做的呢,下面请看步骤:
第一步,先在 onKeyDown 函数中判断 event.getRepeatCount 的次数(实际上长按就是由一系列的onKeyDown事件触发的)
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
case KeyEvent.KEYCODE_VOLUME_DOWN:
case KeyEvent.KEYCODE_VOLUME_UP:
if (mCameraAppView.getVisibility() != View.VISIBLE) {
return false;
}
Log.v(TAG,"onKeyDown event.getRepeatCount() "+event.getRepeatCount());
if (event.getRepeatCount() == 0) {
event.startTracking();
return true;
}
return true;
}
return super.onKeyDown(keyCode, event);
}
如果一直按着不放,通过打 log 可以看到
onKeyDown event.getRepeatCount() 0
onKeyDown event.getRepeatCount() 1
onKeyDown event.getRepeatCount() 2
onKeyDown event.getRepeatCount() 3
onKeyDown event.getRepeatCount() 4
onKeyDown event.getRepeatCount() 5
onKeyDown event.getRepeatCount() 6
onKeyDown event.getRepeatCount() 7
onKeyDown event.getRepeatCount() 8
onKeyDown event.getRepeatCount() 9
onKeyDown event.getRepeatCount() 10
onKeyDown event.getRepeatCount() 11
onKeyDown event.getRepeatCount() 12
……
第二步,重载 onKeyLongPressed 函数,在这个函数中你可以添加你的处理
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
Log.v(TAG,"key long pressed keyCode = "+keyCode);
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_DOWN:
Log.v(TAG,"KEYCODE_VOLUME_UP/DOWN long pressed");
lockLongPressKey = true;
onShutterButtonLongPressed();
return true;
default:
break;
}
return super.onKeyLongPress(keyCode, event);
}
第三步,如果在onKeyUp 函数中也处理了相同的按键的话,那么需要对 onKeyLongPress 和 onKeyUp 中该按键的处理作互斥处理了
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_DOWN:
Log.v(TAG, "onKeyUp event.getRepeatCount() "+event.getRepeatCount());
if(lockLongPressKey){
lockLongPressKey = false;
return true;
}
if (event.getRepeatCount() == 0) {
Log.v(TAG, "prepare for onShutterButtonClick..");
if (mEnableRecordBtn && mReviewImage.getVisibility() != View.VISIBLE
&& mBgLearningMessageFrame.getVisibility() == View.GONE) {
if (mShutterButton != null && mShutterButton.isEnabled()) {
onShutterButtonFocus(false);
collapseCameraControls();//fulin@20121101@fix PD1218 B121027-224
onShutterButtonClick();
}
}
}
if (mCameraAppView.getVisibility() != View.VISIBLE) {
return false;
}
return true;
}
return super.onKeyUp(keyCode, event);
}