Android KeyEvent分发机制

本文详细解析了Android KeyEvent的分发机制,从ViewRootImpl开始,讲解了事件如何从输入队列到视图的传递。重点介绍了当KeyEvent未被消费且涉及方向键时,系统如何通过focusSearch查找并切换焦点。此外,还概述了focusSearch流程,包括focused!=null和focused==null两种情况下的焦点搜索策略。
摘要由CSDN通过智能技术生成

Android KeyEvent分发机制

简介

KeyEvent的分发机制和TouchEvent的分发机制略有不同,KeyEvent向下分发的事件没有被消费,并且KeyCode为KEYCODE_DPAD_LEFT,KEYCODE_DPAD_RIGHT,KEYCODE_DPAD_UP,KEYCODE_DPAD_DOWN,那么会按方向查找下一个控件并获取焦点。

触摸事件分发机制传送门: Android触摸事件分发机制源码分析

分发机制分析

先放上整体流程图,稍后再详细解释:

在这里插入图片描述

ViewRootImpl是所有View的顶层容器,所以从ViewRootImpl着手。ViewRootImpl的dispatchInputEvent()方法,向消息队列发出MSG_DISPATCH_INPUT_EVENT消息,Handler处理消息并调用ViewRootImpl的enqueueInputEvent()方法,将QueuedInputEvent插入到未处理KeyEvent队列的尾部。

enqueueInputEvent()方法具体实现:

void enqueueInputEvent(InputEvent event,InputEventReceiver receiver, int flags, boolean processImmediately) {
   
    adjustInputEventForCompatibility(event);
    QueuedInputEvent q = obtainQueuedInputEvent(event, receiver, flags);

    QueuedInputEvent last = mPendingInputEventTail;
    if (last == null) {
   
        mPendingInputEventHead = q;
        mPendingInputEventTail = q;
    } else {
   
        last.mNext = q;
        mPendingInputEventTail = q;
    }
    mPendingInputEventCount += 1;
    Trace.traceCounter(Trace.TRACE_TAG_INPUT, mPendingInputEventQueueLengthCounterName,mPendingInputEventCount);

    if (processImmediately) {
   
        doProcessInputEvents();
    } else {
   
        scheduleProcessInputEvents();
    }
}

keyEvent插入未处理事件队列后,接下来要处理这些事件了。有两种处理方式,直接处理和向Handler发送异步消息处理。这里只分析直接处理的情况。

doProcessInputEvents()方法遍历未处理事件队列逐个处理:

void doProcessInputEvents() {
   
        // Deliver all pending input events in the queue.
        while (mPendingInputEventHead != null) {
   
            QueuedInputEvent q = mPendingInputEventHead;
            mPendingInputEventHead = q.mNext;
            if (mPendingInputEventHead == null) {
   
                mPendingInputEventTail = null;
            }
            q.mNext = null;

            mPendingInputEventCount -= 1;
            Trace.traceCounter(Trace.TRACE_TAG_INPUT, mPendingInputEventQueueLengthCounterName,
                    mPendingInputEventCount);

            long eventTime = q.mEvent.getEventTimeNano();
            long oldestEventTime = eventTime;
            if (q.mEvent instanceof MotionEvent) {
   
                MotionEvent me = (MotionEvent)q.mEvent;
                if (me.getHistorySize() > 0) {
   
                    oldestEventTime = me.getHistoricalEventTimeNano(0);
                }
            }
            mChoreographer.mFrameInfo.updateInputEventTime(eventTime, oldestEventTime);

            deliverInputEvent(q);
        }

        // We are done processing all input events that we can process right now
        // so we can clear the pending flag immediately.
        if (mProcessInputEventsScheduled) {
   
            mProcessInputEventsScheduled = false;
            mHandler.removeMessages(MSG_PROCESS_INPUT_EVENTS);
        }
    }

QueuedInputEvent交给deliverInputEvent()方法来处理:

private void deliverInputEvent(QueuedInputEvent q) {
   
    Trace.asyncTraceBegin(Trace.TRACE_TAG_VIEW,"deliverInputEvent",
        q.mEvent.getSequenceNumber());
    if (mInputEventConsistencyVerifier != null) {
   
        mInputEventConsistencyVerifier.onInputEvent(q.mEvent, 0);
    }

    InputStage stage;
    if (q.shouldSendToSynthesizer()) {
   
        stage = mSyntheticInputStage;
    } else {
   
        stage = q.shouldSkipIme() ? mFirstPostImeInputStage : mFirstInputStage;
    }

    if (stage != null) {
   
        stage.deliver(q);
    } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值