在一些系統開發中(例如機頂盒)有可能遇到需求不響應鼠標按鍵,在開發中怎麼解決呢?下面我來給大家演示:
1.系統中按鍵的響應都是通過在ViewRootImpl中傳遞給View的,所以要想屏蔽按鍵就要在ViewRootImpl.java中尋找;
2.在setView中sWindowSession.add(mWindow, mSeq, mWindowAttributes,getHostVisibility(), mAttachInfo.mContentInsets,mInputChannel);建立View與WMS的聯系這樣WMS就能把消息傳遞給View了,但是怎麼傳遞的呢?答案是:mInputChannel.
注冊:
[java]
if (mInputChannel != null) {
if (mInputQueueCallback != null) {
mInputQueue = new InputQueue(mInputChannel);
mInputQueueCallback.onInputQueueCreated(mInputQueue);
} else {
InputQueue.registerInputChannel(mInputChannel, mInputHandler,
Looper.myQueue());
}
}
響應回掉處理:
[java]
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);//處理觸摸<span style="font-size:18px">,鼠標,搖桿等消息</span>
}
};
3.下面看dispatchMotion函數:
[java]
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);//在有鼠標點擊<span style="font-size:18px">事件時會調用</span>
} 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函數中處理,具體代碼:
[java]
finishMotionEvent(event, sendDone, true);
return;