Android 中keyEvent的消息处理

2024-01-02 08:08
文章标签 android 处理 消息 keyevent

本文主要是介绍Android 中keyEvent的消息处理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


1. ViewRootImpl.deliverKeyEvent(QueuedInputEvent q)

      1. 如果mView为空或者mAdded为false,就直接调用finishInputEvent。

      2. mView.dispatchKeyEventPreIme(event), 在传递给IME之前做一些预处理。因为对于View来说,如果有输入窗口存在的话,会先将按键消息派发到输入窗口,只有当输入窗口没有处理这个事件,才会派发到真正的视图。因此如果想要在输入法截取事件前处理该消息,则可以重载这个方法去处理一些特定的按键消息。

      3. 如果有IME窗口存在,就把这个传递给IME进行处理。imm.dispatchKeyEvent(mView.getContext(), seq, event, mInputMethodCallback);

      4. deliverKeyEventPostIme(q);  将消息派发给真正的视图。

    private void deliverKeyEvent(QueuedInputEvent q) {final KeyEvent event = (KeyEvent)q.mEvent;if (mInputEventConsistencyVerifier != null) {mInputEventConsistencyVerifier.onKeyEvent(event, 0);}if ((q.mFlags & QueuedInputEvent.FLAG_DELIVER_POST_IME) == 0) {// If there is no view, then the event will not be handled.if (mView == null || !mAdded) {finishInputEvent(q, false);return;}if (LOCAL_LOGV) Log.v(TAG, "Dispatching key " + event + " to " + mView);// Perform predispatching before the IME.if (mView.dispatchKeyEventPreIme(event)) {finishInputEvent(q, true);return;}// Dispatch to the IME before propagating down the view hierarchy.// The IME will eventually call back into handleImeFinishedEvent.if (mLastWasImTarget) {InputMethodManager imm = InputMethodManager.peekInstance();if (imm != null) {final int seq = event.getSequenceNumber();if (DEBUG_IMF) Log.v(TAG, "Sending key event to IME: seq="+ seq + " event=" + event);imm.dispatchKeyEvent(mView.getContext(), seq, event, mInputMethodCallback);return;}}}// Not dispatching to IME, continue with post IME actions.deliverKeyEventPostIme(q);}

1.2  ViewRootImpl.deliverKeyEventPostIme(q);

         1. mView.dispatchKeyEvent(event);  将事件派发到View视图。因为mView就是一个DecorView,所以就是调用DecorView的dispatchKeyEvent的方法。

    private void deliverKeyEventPostIme(QueuedInputEvent q) {final KeyEvent event = (KeyEvent)q.mEvent;... ...// If the key's purpose is to exit touch mode then we consume it and consider it handled.if (checkForLeavingTouchModeAndConsume(event)) {finishInputEvent(q, true);return;}// Make sure the fallback event policy sees all keys that will be delivered to the// view hierarchy.mFallbackEventHandler.preDispatchKeyEvent(event);// Deliver the key to the view hierarchy.if (mView.dispatchKeyEvent(event)) {finishInputEvent(q, true);return;}// If the Control modifier is held, try to interpret the key as a shortcut.if (event.getAction() == KeyEvent.ACTION_DOWN&& event.isCtrlPressed()&& event.getRepeatCount() == 0&& !KeyEvent.isModifierKey(event.getKeyCode())) {if (mView.dispatchKeyShortcutEvent(event)) {finishInputEvent(q, true);return;}}// Apply the fallback event policy.if (mFallbackEventHandler.dispatchKeyEvent(event)) {finishInputEvent(q, true);return;}// Handle automatic focus changes.if (event.getAction() == KeyEvent.ACTION_DOWN) {int direction = 0;switch (event.getKeyCode()) {case KeyEvent.KEYCODE_DPAD_LEFT:if (event.hasNoModifiers()) {direction = View.FOCUS_LEFT;}break;case KeyEvent.KEYCODE_DPAD_RIGHT:if (event.hasNoModifiers()) {direction = View.FOCUS_RIGHT;}break;case KeyEvent.KEYCODE_DPAD_UP:if (event.hasNoModifiers()) {direction = View.FOCUS_UP;}break;case KeyEvent.KEYCODE_DPAD_DOWN:if (event.hasNoModifiers()) {direction = View.FOCUS_DOWN;}break;case KeyEvent.KEYCODE_TAB:if (event.hasNoModifiers()) {direction = View.FOCUS_FORWARD;} else if (event.hasModifiers(KeyEvent.META_SHIFT_ON)) {direction = View.FOCUS_BACKWARD;}break;}if (direction != 0) {View focused = mView.findFocus();if (focused != null) {View v = focused.focusSearch(direction);if (v != null && v != focused) {// do the math the get the interesting rect// of previous focused into the coord system of// newly focused viewfocused.getFocusedRect(mTempRect);if (mView instanceof ViewGroup) {((ViewGroup) mView).offsetDescendantRectToMyCoords(focused, mTempRect);((ViewGroup) mView).offsetRectIntoDescendantCoords(v, mTempRect);}if (v.requestFocus(direction, mTempRect)) {playSoundEffect(SoundEffectConstants.getContantForFocusDirection(direction));finishInputEvent(q, true);return;}}// Give the focused view a last chance to handle the dpad key.if (mView.dispatchUnhandledMove(focused, direction)) {finishInputEvent(q, true);return;}}}}// Key was unhandled.finishInputEvent(q, false);}

1.2.1 DecorView.dispatchKeyEvent(KeyEvent event)

     1. 如果是Down事件, 首先会判断是不是Shortcut事件也就是快捷键按钮或者特殊事件,如果是就会调用dispatchKeyShortcutEvent和performPanelShortcut进行一些处理然后返回。

     2. 如果不是快捷键按钮,就调用Callback Activity的cb.dispatchKeyEvent(event)去处理。Activity的dispatchKeyEvent中其实也还是去调用PhoneWindow中的superDispatchKeyEvent, PhoneWindow最终还是调用mDecor.superDispatchKeyEvent(event);

        @Overridepublic boolean dispatchKeyEvent(KeyEvent event) {final int keyCode = event.getKeyCode();final int action = event.getAction();final boolean isDown = action == KeyEvent.ACTION_DOWN;if (isDown && (event.getRepeatCount() == 0)) {// First handle chording of panel key: if a panel key is held// but not released, try to execute a shortcut in it.if ((mPanelChordingKey > 0) && (mPanelChordingKey != keyCode)) {boolean handled = dispatchKeyShortcutEvent(event);if (handled) {return true;}}// If a panel is open, perform a shortcut on it without the// chorded panel keyif ((mPreparedPanel != null) && mPreparedPanel.isOpen) {if (performPanelShortcut(mPreparedPanel, keyCode, event, 0)) {return true;}}}if (!isDestroyed()) {final Callback cb = getCallback();final boolean handled = cb != null && mFeatureId < 0 ? cb.dispatchKeyEvent(event): super.dispatchKeyEvent(event);if (handled) {return true;}}return isDown ? PhoneWindow.this.onKeyDown(mFeatureId, event.getKeyCode(), event): PhoneWindow.this.onKeyUp(mFeatureId, event.getKeyCode(), event);}

1.2.1.2 DecorView.mDecor.superDispatchKeyEvent(event);

           1. 调用父类的dispatchKeyEvent。

       public boolean superDispatchKeyEvent(KeyEvent event) {if (super.dispatchKeyEvent(event)) {return true;}// Not handled by the view hierarchy, does the action bar want it// to cancel out of something special?if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {final int action = event.getAction();// Back cancels action modes first.if (mActionMode != null) {if (action == KeyEvent.ACTION_UP) {mActionMode.finish();}return true;}// Next collapse any expanded action views.if (mActionBar != null && mActionBar.hasExpandedActionView()) {if (action == KeyEvent.ACTION_UP) {mActionBar.collapseActionView();}return true;}}return false;}


1.2.1.2.1 ViewGroup.dispatchKeyEvent(KeyEvent event)

        1. mPrivateFlags 是在View中定义的,标志当前View的属性。 这里吧的FOCUSED 和HAS_BOUNDS表示当前的View是否是focused和有bounds的。一般的View和ViewGroup都会有HAS_BOUNDS, 而mFocused记录这当前ViewGroup中处于focus的View。

             例如:如果A包含B,B又包含C,如果C是一个View,C的mPrivateFlags就是 FOCUSED | HAS_BOUNDS那么B的mPrivateFlags就是FOCUSED,mFocused就是C,同理A的mPrivateFlags也是FOCUSED,mFocused就是B。

        2. 所以一般情况下,ViewGroup通过mFocused.dispatchKeyEvent()递归把Event传到View。

    public boolean dispatchKeyEvent(KeyEvent event) {if (mInputEventConsistencyVerifier != null) {mInputEventConsistencyVerifier.onKeyEvent(event, 1);}if ((mPrivateFlags & (FOCUSED | HAS_BOUNDS)) == (FOCUSED | HAS_BOUNDS)) {if (super.dispatchKeyEvent(event)) {return true;}} else if (mFocused != null && (mFocused.mPrivateFlags & HAS_BOUNDS) == HAS_BOUNDS) {if (mFocused.dispatchKeyEvent(event)) {return true;}}if (mInputEventConsistencyVerifier != null) {mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);}return false;}

1.2.1.2.1.2 View.dispatchKeyEvent()

            如果mFocused是ViewGroup那么还是调用1.2.1.2.1;如果是View,那么就调用View的dispatchKeyEvent。

            1. 如果有注册Listener,就直接调用Listener的onKey去相应,然后返回。

            2. 如果没有注册Listener,就调用KeyEvent.dispatch去做最后的处理

    public boolean dispatchKeyEvent(KeyEvent event) {if (mInputEventConsistencyVerifier != null) {mInputEventConsistencyVerifier.onKeyEvent(event, 0);}// Give any attached key listener a first crack at the event.//noinspection SimplifiableIfStatementListenerInfo li = mListenerInfo;if (li != null && li.mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED&& li.mOnKeyListener.onKey(this, event.getKeyCode(), event)) {return true;}if (event.dispatch(this, mAttachInfo != null? mAttachInfo.mKeyDispatchState : null, this)) {return true;}if (mInputEventConsistencyVerifier != null) {mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);}return false;}

1.2.1.2.1.2.2 KeyEvent.dispatch

        1. KeyEvent.dispatch会根据Action的类型调用receiver的对应的onXXX函数去处理。(receiver通常都是View)

        2. 如果所有的View都没有处理这个KeyEvent,那么会最终通过KeyEvent调用Activity的onXXX函数。

    public final boolean dispatch(Callback receiver, DispatcherState state,Object target) {switch (mAction) {case ACTION_DOWN: {mFlags &= ~FLAG_START_TRACKING;if (DEBUG) Log.v(TAG, "Key down to " + target + " in " + state+ ": " + this);boolean res = receiver.onKeyDown(mKeyCode, this);if (state != null) {if (res && mRepeatCount == 0 && (mFlags&FLAG_START_TRACKING) != 0) {if (DEBUG) Log.v(TAG, "  Start tracking!");state.startTracking(this, target);} else if (isLongPress() && state.isTracking(this)) {try {if (receiver.onKeyLongPress(mKeyCode, this)) {if (DEBUG) Log.v(TAG, "  Clear from long press!");state.performedLongPress(this);res = true;}} catch (AbstractMethodError e) {}}}return res;}case ACTION_UP:if (DEBUG) Log.v(TAG, "Key up to " + target + " in " + state+ ": " + this);if (state != null) {state.handleUpEvent(this);}return receiver.onKeyUp(mKeyCode, this);case ACTION_MULTIPLE:final int count = mRepeatCount;final int code = mKeyCode;if (receiver.onKeyMultiple(code, count, this)) {return true;}if (code != KeyEvent.KEYCODE_UNKNOWN) {mAction = ACTION_DOWN;mRepeatCount = 0;boolean handled = receiver.onKeyDown(code, this);if (handled) {mAction = ACTION_UP;receiver.onKeyUp(code, this);}mAction = ACTION_MULTIPLE;mRepeatCount = count;return handled;}return false;}return false;}













这篇关于Android 中keyEvent的消息处理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/561848

相关文章

Android 悬浮窗开发示例((动态权限请求 | 前台服务和通知 | 悬浮窗创建 )

《Android悬浮窗开发示例((动态权限请求|前台服务和通知|悬浮窗创建)》本文介绍了Android悬浮窗的实现效果,包括动态权限请求、前台服务和通知的使用,悬浮窗权限需要动态申请并引导... 目录一、悬浮窗 动态权限请求1、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

如何通过Python实现一个消息队列

《如何通过Python实现一个消息队列》这篇文章主要为大家详细介绍了如何通过Python实现一个简单的消息队列,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录如何通过 python 实现消息队列如何把 http 请求放在队列中执行1. 使用 queue.Queue 和 reque

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

使用C++将处理后的信号保存为PNG和TIFF格式

《使用C++将处理后的信号保存为PNG和TIFF格式》在信号处理领域,我们常常需要将处理结果以图像的形式保存下来,方便后续分析和展示,C++提供了多种库来处理图像数据,本文将介绍如何使用stb_ima... 目录1. PNG格式保存使用stb_imagephp_write库1.1 安装和包含库1.2 代码解

C#使用DeepSeek API实现自然语言处理,文本分类和情感分析

《C#使用DeepSeekAPI实现自然语言处理,文本分类和情感分析》在C#中使用DeepSeekAPI可以实现多种功能,例如自然语言处理、文本分类、情感分析等,本文主要为大家介绍了具体实现步骤,... 目录准备工作文本生成文本分类问答系统代码生成翻译功能文本摘要文本校对图像描述生成总结在C#中使用Deep

Spring Boot 整合 ShedLock 处理定时任务重复执行的问题小结

《SpringBoot整合ShedLock处理定时任务重复执行的问题小结》ShedLock是解决分布式系统中定时任务重复执行问题的Java库,通过在数据库中加锁,确保只有一个节点在指定时间执行... 目录前言什么是 ShedLock?ShedLock 的工作原理:定时任务重复执行China编程的问题使用 Shed

解读Redis秒杀优化方案(阻塞队列+基于Stream流的消息队列)

《解读Redis秒杀优化方案(阻塞队列+基于Stream流的消息队列)》该文章介绍了使用Redis的阻塞队列和Stream流的消息队列来优化秒杀系统的方案,通过将秒杀流程拆分为两条流水线,使用Redi... 目录Redis秒杀优化方案(阻塞队列+Stream流的消息队列)什么是消息队列?消费者组的工作方式每

使用C/C++调用libcurl调试消息的方式

《使用C/C++调用libcurl调试消息的方式》在使用C/C++调用libcurl进行HTTP请求时,有时我们需要查看请求的/应答消息的内容(包括请求头和请求体)以方便调试,libcurl提供了多种... 目录1. libcurl 调试工具简介2. 输出请求消息使用 CURLOPT_VERBOSE使用 C

Redis如何使用zset处理排行榜和计数问题

《Redis如何使用zset处理排行榜和计数问题》Redis的ZSET数据结构非常适合处理排行榜和计数问题,它可以在高并发的点赞业务中高效地管理点赞的排名,并且由于ZSET的排序特性,可以轻松实现根据... 目录Redis使用zset处理排行榜和计数业务逻辑ZSET 数据结构优化高并发的点赞操作ZSET 结

微服务架构之使用RabbitMQ进行异步处理方式

《微服务架构之使用RabbitMQ进行异步处理方式》本文介绍了RabbitMQ的基本概念、异步调用处理逻辑、RabbitMQ的基本使用方法以及在SpringBoot项目中使用RabbitMQ解决高并发... 目录一.什么是RabbitMQ?二.异步调用处理逻辑:三.RabbitMQ的基本使用1.安装2.架构