在一些系统开发中(例如机顶盒)有可能遇到需求不响应鼠标按键,在开发中怎么解决呢?下面我来给大家演示:
1.系统中按键的响应都是通过在ViewRootImpl中传递给View的,所以要想屏蔽按键就要在ViewRootImpl.java中寻找;
2.在setView中sWindowSession.add(mWindow, mSeq, mWindowAttributes,getHostVisibility(), mAttachInfo.mContentInsets,mInputChannel);建立View与WMS的联系这样WMS就能把消息传递给View了,但是怎么传递的呢?答案是:mInputChannel.
注册:
if (mInputChannel != null) { if (mInputQueueCallback != null) { mInputQueue = new InputQueue(mInputChannel); mInputQueueCallback.onInputQueueCreated(mInputQueue); } else { InputQueue.registerInputChannel(mInputChannel, mInputHandler, Looper.myQueue()); } }
private final InputHandler mInputHandler = new InputHandler() {
public void handleKey(KeyEvent event, InputQueue.FinishedCallback finishedCallback) {
startInputEvent(finishedCallback);
dispatchKey(event, true);//处理按键
}
public void handleMotion(MotionEvent event, InputQueue.FinishedCallback finishedCallback) {
startInputEvent(finishedCallback);
dispatchMotion(event, true);//处理触摸,鼠标,摇杆等消息
}
};
3.下面看dispatchMotion函数:
private void dispatchMotion(MotionEvent event, boolean sendDone) {
int source = event.getSource();
if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
Log.d(TAG,"----dispatchPointer----");
dispatchPointer(event, sendDone);//在有鼠标点击事件时会调用
} else if ((source & InputDevice.SOURCE_CLASS_TRACKBALL) != 0) {
dispatchTrackball(event, sendDone);
Log.d(TAG,"----dispatchTrackball----");
} else {
dispatchGenericMotion(event, sendDone);
Log.d(TAG,"----dispatchGenericMotion----");
}
}
看dispatchPointer函数,其实里面就是发送了DISPATCH_POINTER消息真正处理是在deliverPointerEvent函数;所以只需要在deliverPointerEvent函数中处理,具体代码:
finishMotionEvent(event, sendDone, true); return;