屏蔽系統鼠標按鍵

在一些系統開發中(例如機頂盒)有可能遇到需求不響應鼠標按鍵,在開發中怎麼解決呢?下面我來給大家演示:

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;






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章