{"id":4333,"date":"2025-04-28T23:14:25","date_gmt":"2025-04-28T15:14:25","guid":{"rendered":"https:\/\/blog.coderfan.org\/?p=4333"},"modified":"2025-04-29T08:22:07","modified_gmt":"2025-04-29T00:22:07","slug":"android12-input-event-dispatch-progress","status":"publish","type":"post","link":"https:\/\/blog.coderfan.org\/en\/android12-input-event-dispatch-progress.html","title":{"rendered":"Android12 Input\u5b50\u7cfb\u7edf\u89e3\u6790"},"content":{"rendered":"<div class='booster-block booster-read-block'>\n                <div class=\"twp-read-time\">\n                \t<i class=\"booster-icon twp-clock\"><\/i> <span>Read Time:<\/span>18 Minute, 27 Second                <\/div>\n\n            <\/div>\n<p class=\"has-text-align-justify\">\u5927\u5bb6\u6709\u6ca1\u6709\u60f3\u8fc7\uff0c\u4e3a\u4ec0\u4e48\u6211\u4eec\u5728\u5b89\u5353\u8bbe\u5907\u4e0a\u70b9\u51fb\u5c4f\u5e55\uff0c\u5bf9\u5e94\u7684\u83dc\u5355\u5c31\u4f1a\u51fa\u73b0\uff0c\u5c4f\u5e55\u4e0a\u7684\u6309\u94ae\u4f1a\u51fa\u73b0\u76f8\u5e94\u7684\u7279\u6548\uff0c\u8fd9\u4e00\u8fc7\u7a0b\u5230\u5e95\u662f\u600e\u4e48\u5b9e\u73b0\u7684\uff0c\u8fd9\u91cc\u5c31\u9700\u8981\u6d89\u53ca\u5230Input\u4e8b\u4ef6\u7684\u5206\u53d1\u4e0e\u5904\u7406\u4e86\u3002\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u5c06\u4f1a\u9488\u5bf9\u8fd9\u4e2a\u95ee\u9898\u8fdb\u884c\u5206\u6790\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Input\u6982\u8ff0<\/h4>\n\n\n\n<p class=\"has-text-align-justify\">\u4ece\u7b2c\u4e00\u6bb5\u4e2d\u6211\u4eec\u5f15\u7533\u51fa\u7684\u95ee\u9898\uff0c\u6211\u4eec\u9700\u8981\u81ea\u4e0b\u800c\u4e0a\u8003\u8651\u8fd9\u4e9b\u95ee\u9898\uff1a<\/p>\n\n\n\n<p class=\"has-text-align-justify\">1.<strong>\u7cfb\u7edfDriver\u5982\u4f55\u8bfb\u53d6\u8bbe\u5907\u4e0a\u7684\u4e8b\u4ef6\uff0c\u8fd9\u4e9b\u8bbe\u5907\u6709\u53ef\u80fd\u662f\u9f20\u6807\u3001\u952e\u76d8\u3001\u89e6\u6478\u5c4f\u7b49<\/strong>\uff1b<\/p>\n\n\n\n<p class=\"has-text-align-justify\">2.<strong>\u4ece\u7cfb\u7edfDriver\u5230Native Framework\u5982\u4f55\u4f20\u9012\uff0c\u4e43\u81f3\u5206\u53d1\u4e8b\u4ef6<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-justify\">3.<strong>\u5206\u53d1\u4e8b\u4ef6\u540e\u5982\u4f55\u5339\u914d\u5230\u5bf9\u5e94\u7684\u7a97\u53e3<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-justify\">4.<strong>\u5728\u591a\u4e2aLayer\u7684\u60c5\u51b5\u4e0b\u5982\u4f55\u4fdd\u8bc1\u540c\u4e00\u533a\u57df\u4e0b\u5bf9\u5e94Layer\u7684\u5904\u7406<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-justify\">5.<strong>\u5bf9\u5e94\u7a97\u53e3\u5bf9\u5e94Layer\u65f6\uff0c\u662f\u5982\u4f55\u5177\u4f53\u5904\u7406Input\u7684<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-justify\">6.<strong>Input\u4e8b\u4ef6\u4e0eView\u7684\u5173\u8054\u5904\u7406\u662f\u5982\u4f55\u5b9e\u73b0\u7684<\/strong><\/p>\n\n\n\n<p class=\"has-text-align-justify\">\u9996\u5148\u6211\u4eec\u5148\u770b\u4e00\u5f20\u56fe\uff0c\u5176\u56ca\u62ec\u4e86Android Input\u5b50\u7cfb\u7edf\u7684\u4e3b\u4f53\u90e8\u5206\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"966\" height=\"673\" src=\"https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/05\/image-2.png\" alt=\"\" class=\"wp-image-4691\" srcset=\"https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/05\/image-2.png 966w, https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/05\/image-2-300x209.png 300w, https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/05\/image-2-768x535.png 768w, https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/05\/image-2-18x12.png 18w\" sizes=\"auto, (max-width: 966px) 100vw, 966px\" \/><\/figure>\n\n\n\n<p>\u8fd9\u91cc\u9762\u6d89\u53ca\u5230\u7684\u51e0\u4e2a\u91cd\u8981\u90e8\u5206\u5982\u4e0b\uff1a<\/p>\n\n\n\n<p class=\"has-text-align-justify\"><strong>SystemServer<\/strong>\uff1aAndroid\u4e2d\u6574\u4e2aInput\u5b50\u7cfb\u7edf\u7684Entry\uff0c\u7531SystemServer\u62c9\u8d77InputManager\u4f7f\u5f97\u6574\u4e2aInput\u5b50\u7cfb\u7edf\u5f00\u59cb\u8fd0\u4f5c\u3002<\/p>\n\n\n\n<p class=\"has-text-align-justify\"><strong>InputManagerService<\/strong>\uff1a\u5411\u4e0b\u4f1a\u63a5\u5165com_android_server_input_InputManagerService,\u8fd9\u90e8\u5206\u5c5e\u4e8e\u662fJNI\u63a5\u53e3\u3002<\/p>\n\n\n\n<p class=\"has-text-align-justify\"><strong>NativeInputManager<\/strong>\uff1a\u5c5e\u4e8e com_android_server_input_InputManagerService \u7684\u6838\u5fc3\u90e8\u5206\uff0c\u6e90\u7801\u4f4d\u4e8eframeworks\/base\/services\/core\/jni\/com_android_server_InputManagerService.cpp,\u5176\u6838\u5fc3\u5b9e\u73b0\u4e3aNativeInputManager\u7c7b\u3002<\/p>\n\n\n\n<p class=\"has-text-align-justify\"><strong>InputManager<\/strong>\uff1a\u5c5e\u4e8eNative C++\u5c42\u7684\u7ba1\u7406\u5165\u53e3<\/p>\n\n\n\n<p class=\"has-text-align-justify\"><strong>InputReader<\/strong>\uff1a\u7531InputManager\u8fdb\u884c\u521b\u5efa\uff0c\u5c06\u4eceEventhub\u4e2d\u83b7\u53d6\u5230\u7684\u4e8b\u4ef6\uff08\u901a\u8fc7\u8c03\u7528Eventhub\u4e2d\u7684getEvents\uff09\u8fdb\u884c\u52a0\u5de5\u5904\u7406\uff0c\u5e76\u5c06\u6570\u636e\u4f20\u5165InputDispatcher\u8fdb\u884c\u5206\u53d1\u3002<\/p>\n\n\n\n<p class=\"has-text-align-justify\"><strong>EventHub<\/strong>\uff1a\u7528\u4e8e\u7ba1\u7406\/dev\/input\u76ee\u5f55\u4e0b\u7684\u5b57\u7b26\u8bbe\u5907\uff0c\u901a\u8fc7epoll\u673a\u5236\u5b9e\u73b0\u4e8b\u4ef6\u76d1\u542c\uff0c\u5e76\u901a\u8fc7ioctl\uff0cread\u7b49\u7cfb\u7edf\u8c03\u7528\u83b7\u53d6input\u8bbe\u5907\u4fe1\u606f\u5e76\u83b7\u53d6input\u8bbe\u5907\u7684\u5177\u4f53\u4e8b\u4ef6\u3002<\/p>\n\n\n\n<p class=\"has-text-align-justify\">\u8fd9\u91cc\u6211\u4eec\u6458\u5f55\u90e8\u5206\u5173\u952e\u6027\u4ee3\u7801\uff0c\u6765\u4e32\u8054\u6574\u4e2a\u8c03\u7528\u94fe\u3002<\/p>\n\n\n\n<p><strong>\u9996\u5148\u662f\u6574\u4e2aInput\u7cfb\u7edf\u7684\u521d\u59cb\u5316\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">\/\/frameworks\/base\/services\/java\/com\/android\/server\/SystemServer.java \nprivate void startOtherServices(@NonNull TimingsTraceAndSlog t) {\n   t.traceBegin(\"startOtherServices\");\n   final Context context = mSystemContext;\n   DynamicSystemService dynamicSystem = null;\n   IStorageManager storageManager = null;\n   NetworkManagementService networkManagement = null;\n   IpSecService ipSecService = null;\n   VpnManagerService vpnManager = null;\n   VcnManagementService vcnManagement = null;\n   NetworkStatsService networkStats = null;\n   NetworkPolicyManagerService networkPolicy = null;\n   NsdService serviceDiscovery = null;\n   WindowManagerService wm = null;\n   SerialService serial = null;\n   NetworkTimeUpdateService networkTimeUpdater = null;\n   InputManagerService inputManager = null;\n   TelephonyRegistry telephonyRegistry = null;\n   ConsumerIrService consumerIr = null;\n   MmsServiceBroker mmsService = null;\n   HardwarePropertiesManagerService hardwarePropertiesService = null;\n   PacProxyService pacProxyService = null;\n   ......\n   \/\/\u542f\u52a8framwork Java InputManagerService\n   t.traceBegin(\"StartInputManagerService\");\n   inputManager = new InputManagerService(context);\n   t.traceEnd();\n   .....\n}\n\n\/\/frameworks\/base\/services\/core\/java\/com\/android\/server\/input\/InputManagerService.java \npublic InputManagerService(Context context) {\n        this.mContext = context;\n        this.mHandler = new InputManagerHandler(DisplayThread.get().getLooper());\n\n        mStaticAssociations = loadStaticInputPortAssociations();\n        mUseDevInputEventForAudioJack =\n                \n        context.getResources().getBoolean(R.bool.config_useDevInputEventForAudioJack);\n        Slog.i(TAG, \"Initializing input manager, mUseDevInputEventForAudioJack=\"\n                + mUseDevInputEventForAudioJack);\n       \/\/\u901a\u8fc7nativeInit\n        mPtr = nativeInit(this, mContext, mHandler.getLooper().getQueue());\n\n        String doubleTouchGestureEnablePath = context.getResources().getString(\n                R.string.config_doubleTouchGestureEnableFile);\n        mDoubleTouchGestureEnableFile = \n        TextUtils.isEmpty(doubleTouchGestureEnablePath) ? null :\n        new File(doubleTouchGestureEnablePath);\n\n        LocalServices.addService(InputManagerInternal.class, new LocalService());\n }\n\n\/\/frameworks\/base\/services\/core\/jni\/com_android_server_input_InputManagerService.cpp \nstatic jlong nativeInit(JNIEnv* env, jclass \/* clazz *\/,\n        jobject serviceObj, jobject contextObj, jobject messageQueueObj) {\n    sp&lt;MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);\n    if (messageQueue == nullptr) {\n        jniThrowRuntimeException(env, \"MessageQueue is not initialized.\");\n        return 0;\n    }\n    \/\/\u901a\u8fc7new NativeInputManager\u5bf9\u8c61\u8fdb\u884cnative framework \u521d\u59cb\u5316\uff0c\u4ece\u800c\u8fdb\u5165\u5230inputfinger\n    NativeInputManager* im = new NativeInputManager(contextObj, serviceObj,\n            messageQueue->getLooper());\n    im->incStrong(0);\n    return reinterpret_cast&lt;jlong>(im);\n}\n\n\/\/frameworks\/base\/services\/core\/jni\/com_android_server_input_InputManagerService.cpp\nNativeInputManager::NativeInputManager(jobject contextObj,\n        jobject serviceObj, const sp&lt;Looper>&amp; looper) :\n        mLooper(looper), mInteractive(true) {\n    JNIEnv* env = jniEnv();\n\n    mServiceObj = env->NewGlobalRef(serviceObj);\n\n    {\n        AutoMutex _l(mLock);\n        mLocked.systemUiLightsOut = false;\n        mLocked.pointerSpeed = 0;\n        mLocked.pointerGesturesEnabled = true;\n        mLocked.showTouches = false;\n        mLocked.pointerDisplayId = ADISPLAY_ID_DEFAULT;\n    }\n    mInteractive = true;\n\n    InputManager* im = new InputManager(this, this);\n    mInputManager = im;\n    defaultServiceManager()->addService(String16(\"inputflinger\"), im);\n}\n\n\/\/frameworks\/native\/services\/inputflinger\/InputManager.cpp\nInputManager::InputManager(\n        const sp&lt;InputReaderPolicyInterface>&amp; readerPolicy,\n        const sp&lt;InputDispatcherPolicyInterface>&amp; dispatcherPolicy) {\n    \/\/\u521b\u5efaInputDispatcher\uff0c\u8be5\u5bf9\u8c61\u4e3b\u8981\u7528\u4e8e\u4e8b\u4ef6\u5206\u53d1\n    mDispatcher = createInputDispatcher(dispatcherPolicy);\n    \/\/\n    mClassifier = new InputClassifier(mDispatcher);\n    \/\/\u521b\u5efaInputReader\uff0c\u8be5\u5bf9\u8c61\u4e3b\u8981\u7528\u4e8e\u4e8b\u4ef6\u8bfb\u53d6\n    mReader = createInputReader(readerPolicy, mClassifier);\n}\n\n\/\/frameworks\/native\/services\/inputflinger\/reader\/InputReaderFactory.cpp \nsp&lt;InputReaderInterface> createInputReader(const sp&lt;InputReaderPolicyInterface>&amp; policy,\n                                           const sp&lt;InputListenerInterface>&amp; listener) {\n    return new InputReader(std::make_unique&lt;EventHub>(), policy, listener);\n}\n\n\/\/frameworks\/native\/services\/inputflinger\/reader\/InputReader.cpp\nInputReader::InputReader(std::shared_ptr&lt;EventHubInterface> eventHub,\n                         const sp&lt;InputReaderPolicyInterface>&amp; policy,\n                         const sp&lt;InputListenerInterface>&amp; listener)\n      : mContext(this),\n        mEventHub(eventHub),\n        mPolicy(policy),\n        mGlobalMetaState(0),\n        mLedMetaState(AMETA_NUM_LOCK_ON),\n        mGeneration(1),\n        mNextInputDeviceId(END_RESERVED_ID),\n        mDisableVirtualKeysTimeout(LLONG_MIN),\n        mNextTimeout(LLONG_MAX),\n        mConfigurationChangesToRefresh(0) {\n    mQueuedListener = new QueuedInputListener(listener);\n\n    { \/\/ acquire lock\n        std::scoped_lock _l(mLock);\n\n        refreshConfigurationLocked(0);\n        updateGlobalMetaStateLocked();\n    } \/\/ release lock\n}\n\n\/\/frameworks\/native\/services\/inputflinger\/reader\/EventHub.cpp\nEventHub::EventHub(void)\n      : mBuiltInKeyboardId(NO_BUILT_IN_KEYBOARD),\n        mNextDeviceId(1),\n        mControllerNumbers(),\n        mNeedToSendFinishedDeviceScan(false),\n        mNeedToReopenDevices(false),\n        mNeedToScanDevices(true),\n        mPendingEventCount(0),\n        mPendingEventIndex(0),\n        mPendingINotify(false) {\n\n       .....\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\"><strong>\u521d\u59cb\u5316\u5b8c\u6210\u4e4b\u540e\uff0c\u901a\u8fc7\u4e00\u8fde\u4e32\u7684start\u63a5\u53e3\uff0c\u5f00\u59cbInput\u5b50\u7cfb\u7edf\u7684\u8fd0\u884c<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">\/\/frameworks\/base\/services\/java\/com\/android\/server\/SystemServer.java \nprivate void startOtherServices(@NonNull TimingsTraceAndSlog t) {\n  .....\n  t.traceBegin(\"StartInputManager\");\n  inputManager.setWindowManagerCallbacks(wm.getInputManagerCallback());\n  \/\/\u901a\u8fc7start\u5f00\u59cb\u8fd0\u884c\n  inputManager.start();\n  t.traceEnd();\n  .....\n}\n\/\/frameworks\/base\/services\/core\/java\/com\/android\/server\/input\/InputManagerService.java\npublic void start() {\n        Slog.i(TAG, \"Starting input manager\");\n        \/\/\u901a\u8fc7nativeStart\u8fdb\u884c\u542f\u52a8\n        nativeStart(mPtr);\n\n        \/\/ Add ourself to the Watchdog monitors.\n        Watchdog.getInstance().addMonitor(this);\n\n        registerPointerSpeedSettingObserver();\n        registerShowTouchesSettingObserver();\n        registerAccessibilityLargePointerSettingObserver();\n        registerLongPressTimeoutObserver();\n        registerMaximumObscuringOpacityForTouchSettingObserver();\n        registerBlockUntrustedTouchesModeSettingObserver();\n\n        mContext.registerReceiver(new BroadcastReceiver() {\n            @Override\n            public void onReceive(Context context, Intent intent) {\n                updatePointerSpeedFromSettings();\n                updateShowTouchesFromSettings();\n                updateAccessibilityLargePointerFromSettings();\n                updateDeepPressStatusFromSettings(\"user switched\");\n            }\n        }, new IntentFilter(Intent.ACTION_USER_SWITCHED), null, mHandler);\n\n        updatePointerSpeedFromSettings();\n        updateShowTouchesFromSettings();\n        updateAccessibilityLargePointerFromSettings();\n        updateDeepPressStatusFromSettings(\"just booted\");\n        updateMaximumObscuringOpacityForTouchFromSettings();\n        updateBlockUntrustedTouchesModeFromSettings();\n}\n\/\/frameworks\/base\/services\/core\/jni\/com_android_server_input_InputManagerService.cpp \nstatic void nativeStart(JNIEnv* env, jclass \/* clazz *\/, jlong ptr) {\n    NativeInputManager* im = reinterpret_cast&lt;NativeInputManager*>(ptr);\n\n    status_t result = im->getInputManager()->start();\n    if (result) {\n        jniThrowRuntimeException(env, \"Input manager could not be started.\");\n    }\n}\n\/\/frameworks\/native\/services\/inputflinger\/InputManager.cpp\nstatus_t InputManager::start() {\n    \/\/\u5f00\u542fdispatcher\n    status_t result = mDispatcher->start();\n    if (result) {\n        ALOGE(\"Could not start InputDispatcher thread due to error %d.\", result);\n        return result;\n    }\n    \/\/\u5f00\u542freader\n    result = mReader->start();\n    if (result) {\n        ALOGE(\"Could not start InputReader due to error %d.\", result);\n        mDispatcher->stop();\n        return result;\n    }\n    return OK;\n}\n\/\/frameworks\/native\/services\/inputflinger\/dispatcher\/InputDispatcher.cpp\nstatus_t InputDispatcher::start() {\n    if (mThread) {\n        return ALREADY_EXISTS;\n    }\n    mThread = std::make_unique&lt;InputThread>(\n            \"InputDispatcher\", [this]() { dispatchOnce(); }, [this]() { mLooper->wake(); });\n    return OK;\n}\n\/\/frameworks\/native\/services\/inputflinger\/reader\/InputReader.cpp\nstatus_t InputReader::start() {\n    if (mThread) {\n        return ALREADY_EXISTS;\n    }\n    mThread = std::make_unique&lt;InputThread>(\n            \"InputReader\", [this]() { loopOnce(); }, [this]() { mEventHub->wake(); });\n    return OK;\n}\n<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u5728\u6211\u4eec\u81ea\u4e0a\u800c\u4e0b\u4e86\u89e3\u4e86\u6574\u4e2aInput\u7cfb\u7edf\u7684\u521d\u59cb\u5316\u4e0e\u8fd0\u884c\u6d41\u7a0b\u540e\uff0c\u63a5\u4e0b\u6765\u6211\u4eec\u9488\u5bf9\u6bcf\u4e2a\u5355\u72ec\u7684\u6a21\u5757\u6765\u8fdb\u884c\u5206\u6790\uff0c\u53bb\u4e86\u89e3\u8fd9\u91cc\u9762\u66f4\u591a\u7684\u7ec6\u8282\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e8b\u4ef6\u8bfb\u53d6-InputReader<\/h4>\n\n\n\n<p class=\"has-text-align-justify\">\u4ece\u540d\u5b57\u4e0a\u6211\u4eec\u5c31\u80fd\u5927\u6982\u77e5\u9053Eventhub\u7684\u5177\u4f53\u4f5c\u7528\uff0c\u8fd9\u91cc\u6211\u4eec\u5148\u770b\u770bEventhub\u7684\u521d\u59cb\u5316\u8fc7\u7a0b\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"cpp\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">\/\/frameworks\/native\/services\/inputflinger\/reader\/EventHub.cpp\nEventHub::EventHub(void)\n      : mBuiltInKeyboardId(NO_BUILT_IN_KEYBOARD),\n        mNextDeviceId(1),\n        mControllerNumbers(),\n        mNeedToSendFinishedDeviceScan(false),\n        mNeedToReopenDevices(false),\n        mNeedToScanDevices(true),\n        mPendingEventCount(0),\n        mPendingEventIndex(0),\n        mPendingINotify(false) {\n    ensureProcessCanBlockSuspend();\n    mEpollFd = epoll_create1(EPOLL_CLOEXEC);\n    LOG_ALWAYS_FATAL_IF(mEpollFd &lt; 0, \"Could not create epoll instance: %s\", strerror(errno));\n    mINotifyFd = inotify_init();\n    mInputWd = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE);\n    LOG_ALWAYS_FATAL_IF(mInputWd &lt; 0, \"Could not register INotify for %s: %s\", DEVICE_PATH,\n                        strerror(errno));\n    if (isV4lScanningEnabled()) {\n        mVideoWd = inotify_add_watch(mINotifyFd, VIDEO_DEVICE_PATH, IN_DELETE | IN_CREATE);\n        LOG_ALWAYS_FATAL_IF(mVideoWd &lt; 0, \"Could not register INotify for %s: %s\",\n                            VIDEO_DEVICE_PATH, strerror(errno));\n    } else {\n        mVideoWd = -1;\n        ALOGI(\"Video device scanning disabled\");\n    }\n    struct epoll_event eventItem = {};\n    eventItem.events = EPOLLIN | EPOLLWAKEUP;\n    eventItem.data.fd = mINotifyFd;\n    int result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mINotifyFd, &amp;eventItem);\n    LOG_ALWAYS_FATAL_IF(result != 0, \"Could not add INotify to epoll instance.  errno=%d\", errno);\n    int wakeFds[2];\n    result = pipe(wakeFds);\n    LOG_ALWAYS_FATAL_IF(result != 0, \"Could not create wake pipe.  errno=%d\", errno);\n    mWakeReadPipeFd = wakeFds[0];\n    mWakeWritePipeFd = wakeFds[1];\n    result = fcntl(mWakeReadPipeFd, F_SETFL, O_NONBLOCK);\n    LOG_ALWAYS_FATAL_IF(result != 0, \"Could not make wake read pipe non-blocking.  errno=%d\",\n                        errno);\n    result = fcntl(mWakeWritePipeFd, F_SETFL, O_NONBLOCK);\n    LOG_ALWAYS_FATAL_IF(result != 0, \"Could not make wake write pipe non-blocking.  errno=%d\",\n                        errno);\n    eventItem.data.fd = mWakeReadPipeFd;\n    result = epoll_ctl(mEpollFd, EPOLL_CTL_ADD, mWakeReadPipeFd, &amp;eventItem);\n    LOG_ALWAYS_FATAL_IF(result != 0, \"Could not add wake read pipe to epoll instance.  errno=%d\",\n                        errno);\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u5728Eventhub\u521d\u59cb\u5316\u8fc7\u7a0b\u4e2d\uff0c\u6700\u91cd\u8981\u7684\u662f\u5229\u7528\u4e86inotify\u4e0eepoll\u4e24\u4e2a\u7cfb\u7edf\u8c03\u7528\u3002\u524d\u8005\u7528\u4e8e\u76d1\u542c\/dev\/input\u4e0b\u7684input\u8bbe\u5907\u8282\u70b9\u6dfb\u52a0\u6216\u8005\u5220\u9664\u7684\u53d8\u5316\uff0c\u540e\u8005\u7528\u4e8e\u76d1\u542c\u67d0\u4e2a\u5177\u4f53Input\u8bbe\u5907\u8282\u70b9\uff0c\u5177\u4f53\u53ef\u89c1\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"cpp\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">\/\/frameworks\/native\/services\/inputflinger\/reader\/EventHub.cpp\nEventHub::EventHub(void){\n.....\n    \/\/\u901a\u8fc7inotify_init\u63a5\u53e3\u521b\u5efainotify\u5b9e\u4f8b\uff0c\u8fd4\u56de\u503c\u4e3a\u6587\u4ef6\u63cf\u8ff0\u7b26\n    mINotifyFd = inotify_init();\n    \/\/\u901a\u8fc7inotify_add_watch\u63a5\u53e3\u5c06\/dev\/input\u76ee\u5f55\u52a0\u5165\u76d1\u542c\uff0c\u8fd4\u56de\u503c\u4e3a\u53e6\u5916\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\uff0c\u79f0\u4e3aWatch \n    \/\/descriptor,\u5f53\u5b58\u5728input\u8bbe\u5907\u8282\u70b9\u6dfb\u52a0\u6216\u5220\u9664\u65f6\uff0c\u5728epoll\u4e2d\u4f1a\u76d1\u542c\u5230\u8be5\u4fe1\u606f\n    mInputWd = inotify_add_watch(mINotifyFd, DEVICE_PATH, IN_DELETE | IN_CREATE);\n    LOG_ALWAYS_FATAL_IF(mInputWd &lt; 0, \"Could not register INotify for %s: %s\", DEVICE_PATH,strerror(errno));\n    if (isV4lScanningEnabled()) {\n        mVideoWd = inotify_add_watch(mINotifyFd, VIDEO_DEVICE_PATH, IN_DELETE | IN_CREATE);\n        LOG_ALWAYS_FATAL_IF(mVideoWd &lt; 0, \"Could not register INotify for %s: %s\",\n                            VIDEO_DEVICE_PATH, strerror(errno));\n    } else {\n        mVideoWd = -1;\n        ALOGI(\"Video device scanning disabled\");\n    }\n......\n}\n\/\/frameworks\/native\/services\/inputflinger\/reader\/EventHub.cpp\nstatus_t EventHub::readNotifyLocked() {\n......\n    ALOGV(\"EventHub::readNotify nfd: %d\\n\", mINotifyFd);\n    \/\/\u8bfb\u53d6inotify event\n    res = read(mINotifyFd, event_buf, sizeof(event_buf));\n    if (res &lt; (int)sizeof(*event)) {\n        if (errno == EINTR) return 0;\n        ALOGW(\"could not get event, %s\\n\", strerror(errno));\n        return -1;\n    }\n    while (res >= (int)sizeof(*event)) {\n        event = (struct inotify_event*)(event_buf + event_pos);\n        if (event->len) {\n            \/\/\u5982\u679cinotify event\u4e2d\u6240\u643a\u5e26\u7684watch descriptor\u4e3a\u5bf9\u5e94\u7684input\u8bbe\u5907\u76ee\u5f55\n            \/\/\u5219\u8fdb\u884cCREATE \u6216 DELETE\u7684\u5224\u65ad\n            if (event->wd == mInputWd) {\n                std::string filename = std::string(DEVICE_PATH) + \"\/\" + event->name;\n                \/\/\u5982\u679c\u662f\u65b0\u5efa\u8bbe\u5907\u8282\u70b9\uff0c\u8fdb\u884c\u5904\u7406\n                if (event->mask &amp; IN_CREATE) {\n                    openDeviceLocked(filename);\n                } else {\n                    ALOGI(\"Removing device '%s' due to inotify event\\n\", filename.c_str());\n                    closeDeviceByPathLocked(filename);\n                }\n}\n......\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c\u5f53\/dev\/input\u76ee\u5f55\u4e0b\u521b\u5efa\u4e86\u65b0\u7684\u8bbe\u5907\u8282\u70b9\u65f6\uff0c\u5c06\u4f1a\u8fdb\u5165openDeviceLocked\u51fd\u6570\uff0c\u5728\u8be5\u51fd\u6570\u5185\u4f1a\u901a\u8fc7ioctl\u63a5\u53e3\u5b8c\u6210\u8bbe\u5907\u4fe1\u606f\u7684\u8bfb\u53d6\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"cpp\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">void EventHub::openDeviceLocked(const std::string&amp; devicePath) {\n\/\/open\u7cfb\u7edf\u8c03\u7528\nint fd = open(devicePath.c_str(), O_RDWR | O_CLOEXEC | O_NONBLOCK);\n.....\n\/\/ Get device name.\u83b7\u53d6\u8bbe\u5907\u540d\u79f0\nchar buffer[80];\nioctl(fd, EVIOCGNAME(sizeof(buffer) - 1), &amp;buffer)\n\/\/ Get device driver version.\u83b7\u53d6\u9a71\u52a8\u7248\u672c\nint driverVersion;\nioctl(fd, EVIOCGVERSION, &amp;driverVersion)\n\/\/ Get device identifier.\u83b7\u53d6\u8bbe\u5907\u6807\u8bc6\nstruct input_id inputId;\nioctl(fd, EVIOCGID, &amp;inputId)\n\/\/ Get device physical location.\nioctl(fd, EVIOCGPHYS(sizeof(buffer) - 1), &amp;buffer)\n\/\/ Get device unique id.\nioctl(fd, EVIOCGUNIQ(sizeof(buffer) - 1), &amp;buffer)\n\/\/ Load the configuration file for the device.\u52a0\u8f7dInput\u8bbe\u5907\u914d\u7f6e\ndevice->loadConfigurationLocked();\n.....\ndevice->configureFd();\n.....\naddDeviceLocked(std::move(device));\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u8fd9\u91cc\u9762\u6bd4\u8f83\u91cd\u8981\u7684\u4e00\u4e9b\u6b65\u9aa4\u5305\u62ecInput\u8bbe\u5907\u7684\u914d\u7f6e\u6587\u4ef6\u52a0\u8f7d\u89e3\u6790\uff0c\u9488\u5bf9\u7279\u5b9a\u8bbe\u5907\u7684\u4e00\u4e9b\u8bbe\u7f6e\u4ee5\u53ca\u8bbe\u5907\u52a0\u8f7d\u5b8c\u6210\u540e\u7684\u5904\u7406\uff0c\u91cd\u70b9\u5305\u62ecloadConfigurationLocked\u51fd\u6570\u3001configureFd\u51fd\u6570\u548caddDeviceLocked\u51fd\u6570\uff0c\u5176\u6e90\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"cpp\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">\/\/frameworks\/native\/services\/inputflinger\/reader\/EventHub.cpp\nvoid EventHub::Device::loadConfigurationLocked()\n{\n   configurationFile=getInputDeviceConfigurationFilePathByDeviceIdentifier(identifier,\n                                                                       InputDeviceConfigurationFileType::\n                                                                             CONFIGURATION);\n    if (configurationFile.empty())\n    {\n        ALOGD(\"No input device configuration file found for device '%s'.\", identifier.name.c_str());\n    }\n    else\n    {\n        android::base::Result&lt;std::unique_ptr&lt;PropertyMap>> propertyMap =\n                     PropertyMap::load(configurationFile.c_str());\n        if (!propertyMap.ok())\n        {\n            ALOGE(\"Error loading input device configuration file for device '%s'.  \"\n                  \"Using default configuration.\",\n                  identifier.name.c_str());\n        }\n        else\n        {\n            configuration = std::move(*propertyMap);\n        }\n    }\n}\n\/\/frameworks\/native\/libs\/input\/InputDevice.cpp \nstd::string getInputDeviceConfigurationFilePathByDeviceIdentifier(\n        const InputDeviceIdentifier&amp; deviceIdentifier,\n        InputDeviceConfigurationFileType type) {\n    if (deviceIdentifier.vendor !=0 &amp;&amp; deviceIdentifier.product != 0) {\n        if (deviceIdentifier.version != 0) {\n            \/\/ Try vendor product version.\n            std::string versionPath = getInputDeviceConfigurationFilePathByName(\n                    StringPrintf(\"Vendor_%04x_Product_%04x_Version_%04x\",\n                            deviceIdentifier.vendor, deviceIdentifier.product,\n                            deviceIdentifier.version),\n                    type);\n            if (!versionPath.empty()) {\n                return versionPath;\n            }\n        }\n\n        \/\/ Try vendor product.\n        std::string productPath = getInputDeviceConfigurationFilePathByName(\n                StringPrintf(\"Vendor_%04x_Product_%04x\",\n                        deviceIdentifier.vendor, deviceIdentifier.product),\n                type);\n        if (!productPath.empty()) {\n            return productPath;\n        }\n    }\n\n    \/\/ Try device name.\n    return getInputDeviceConfigurationFilePathByName(deviceIdentifier.getCanonicalName(), type);\n}\n\n\/\/\u9488\u5bf9Keyboard\u952e\u76d8\uff0c\u8bbe\u7f6erepeatRate;\u9488\u5bf9Sensor\u8bbe\u5907\uff0c\u8bbe\u7f6eTimestamp\u7684\u65f6\u949f\u6e90\nvoid EventHub::Device::configureFd() {                                                                                                                                                                      \n    \/\/ Set fd parameters with ioctl, such as key repeat, suspend block, and clock type\n    if (classes.test(InputDeviceClass::KEYBOARD)) {\n        \/\/ Disable kernel key repeat since we handle it ourselves\n        unsigned int repeatRate[] = {0, 0};\n        if (ioctl(fd, EVIOCSREP, repeatRate)) {\n            ALOGW(\"Unable to disable kernel key repeat for %s: %s\", path.c_str(), strerror(errno));\n        }    \n    }    \n\n    \/\/ Tell the kernel that we want to use the monotonic clock for reporting \n    \/\/ timestamps\n    \/\/ associated with input events.  This is important because the input system\n    \/\/ uses the timestamps extensively and assumes they were recorded using the \n    \/\/ monotonic clock.\n    int clockId = CLOCK_MONOTONIC;\n    if (classes.test(InputDeviceClass::SENSOR)) {\n        \/\/ Each new sensor event should use the same time base as\n        \/\/ SystemClock.elapsedRealtimeNanos().\n        clockId = CLOCK_BOOTTIME;\n    }    \n    bool usingClockIoctl = !ioctl(fd, EVIOCSCLOCKID, &amp;clockId);\n    ALOGI(\"usingClockIoctl=%s\", toString(usingClockIoctl));\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u5728Input\u8bbe\u5907\u7684\u914d\u7f6e\u6587\u4ef6\u52a0\u8f7d\u8fc7\u7a0b\u4e2d\uff0c\u9996\u5148\u4f1a\u5728\u7cfb\u7edf\u76ee\u5f55\/system\/product\u3001\/system\/system_ext\u3001\/system\/odm\u4e0e\/system\/vendor\u4e0b\u8fdb\u884c\u67e5\u627e\uff0c\u67e5\u627e\u65f6\u4f9d\u636eFILE_DIR+device_name+FILE_EXTENSION\u8fd9\u6837\u7684\u5f62\u5f0f\u8fdb\u884c\uff0c\u5176\u4e2dFILE_DIR\u5b9a\u4e49\u6b21\u7ea7\u76ee\u5f55\u7684\u540d\u79f0\uff0c\u5176\u53ef\u9009\u503c\u4e3aidc\u3001keylayouts\u3001keychars\uff0c\u76f8\u5e94\u7684FILE_EXTENSION\u5b9a\u4e49\u914d\u7f6e\u6587\u4ef6\u540e\u7f00\u540d\uff0c\u5176\u53ef\u9009\u503c\u4e3a.idc\u3001.kl\u3001.kcm\u3002\u8fd9\u4e09\u79cd\u7c7b\u578b\u7684\u914d\u7f6e\u6587\u4ef6\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7c7b\u578b\u7684input\u8bbe\u5907\uff1a<\/p>\n\n\n\n<p class=\"has-text-align-justify\"><strong>*.kl:\u6309\u952e\u5e03\u5c40\u914d\u7f6e<\/strong>\uff0c\u7528\u4e8e\u5c06Linux \u6309\u952e\u4ee3\u7801\u548c\u5750\u6807\u8f74\u4ee3\u7801\u6620\u5c04\u5230 Android\u7cfb\u7edf\uff1b\u901a\u4fd7\u70b9\u6765\u8bb2\uff0c\u5c31\u597d\u6bd4\u6211\u4eec\u628a\u952e\u76d8\u4e0a\u7684\u6bcf\u4e00\u4e2a\u6309\u952e\u90fd\u8fdb\u884c\u4e00\u4e2a\u7f16\u53f7\uff0c\u8be5\u7f16\u53f7\u90fd\u6709\u4e00\u4e2a\u503c\u4e0e\u4e4b\u5bf9\u5e94\uff0c\u7528\u6237\u6001\u62ff\u5230\u8fd9\u4e2a\u7f16\u53f7\uff0c\u4e5f\u5c31\u77e5\u9053\u4e86\u5bf9\u5e94\u6309\u4e0b\u54ea\u4e2a\u952e\uff0c\u7531\u6b64\u5b8c\u6210\u540e\u7eed\u5904\u7406\u3002Android\u7cfb\u7edf\u4f1a\u5728\/odm\/usr\/keylayout\u3001\/vendor\/usr\/keylayout\u3001\/system\/usr\/keylayout\u3001\/data\/system\/devices\/keylayout\u5185\u8fdb\u884c\u67e5\u627e\uff0c\u7cfb\u7edf\u9ed8\u8ba4\u7684\u914d\u7f6e\u6587\u4ef6\u4f4d\u4e8e \/system\/usr\/keylayout \u5185\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">trout_x86:\/system\/usr\/keylayout # ls -la\ntotal 692\ndrwxr-xr-x 2 root root 8192 2009-01-01 08:00 .\ndrwxr-xr-x 7 root root 4096 2009-01-01 08:00 ..\n-rw-r--r-- 1 root root  811 2009-01-01 08:00 AVRCP.kl\n-rw-r--r-- 1 root root 9453 2009-01-01 08:00 Generic.kl\n-rw-r--r-- 1 root root  810 2009-01-01 08:00 Vendor_0079_Product_0011.kl\n-rw-r--r-- 1 root root 1644 2009-01-01 08:00 Vendor_0079_Product_18d4.kl\n-rw-r--r-- 1 root root 1645 2009-01-01 08:00 Vendor_044f_Product_b326.kl\n-rw-r--r-- 1 root root 1543 2009-01-01 08:00 Vendor_045e_Product_028e.kl\n-rw-r--r-- 1 root root 1644 2009-01-01 08:00 Vendor_045e_Product_028f.kl\n-rw-r--r-- 1 root root 1548 2009-01-01 08:00 Vendor_045e_Product_02a1.kl\n-rw-r--r-- 1 root root 1568 2009-01-01 08:00 Vendor_045e_Product_02d1.kl\n-rw-r--r-- 1 root root 1568 2009-01-01 08:00 Vendor_045e_Product_02dd.kl\n-rw-r--r-- 1 root root 1402 2009-01-01 08:00 Vendor_045e_Product_02e0.kl<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u5176\u4e2dGeneral.kl\u662f\u901a\u7528\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u770b\u6587\u4ef6\u5185\u7684\u5177\u4f53\u5185\u5bb9\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\"># Copyright (C) 2010 The Android Open Source Project\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#      http:\/\/www.apache.org\/licenses\/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n#\n# Generic key layout file for full alphabetic US English PC style external keyboards.\n#\n# This file is intentionally very generic and is intended to support a broad range of keyboards.\n# Do not edit the generic key layout to support a specific keyboard; instead, create\n# a new key layout file with the required keyboard configuration.\n#\n\nkey 1     ESCAPE\nkey 2     1\nkey 3     2\nkey 4     3\nkey 5     4\nkey 6     5\nkey 7     6\nkey 8     7\nkey 9     8\nkey 10    9\nkey 11    0\nkey 12    MINUS\nkey 13    EQUALS\nkey 14    DEL\nkey 15    TAB<\/code><\/pre>\n\n\n\n<p>\u66f4\u591a\u4fe1\u606f\u53ef\u4ee5\u53c2\u8003\u8be5<a href=\"https:\/\/source.android.com\/docs\/core\/interaction\/input\/key-layout-files\" class=\"ek-link\">Link<\/a>\u3002<\/p>\n\n\n\n<p class=\"has-text-align-justify\">*.kcm:<strong>\u6309\u952e\u5b57\u7b26\u6620\u5c04\u914d\u7f6e<\/strong>\uff0c\u7528\u4e8e\u5c06Android\u7684\u6309\u952e\u952e\u503c\uff08\u5305\u542b\u8f85\u52a9\u952e\uff09\u6620\u5c04\u4e3aUnicode\u5b57\u7b26\u3002\u4e5f\u5c31\u662f\u5f53\u4f60\u5728\u952e\u76d8\u4e0a\u6309\u538b\u67d0\u4e2a\u952e\u65f6\uff0c\u4f60\u5728\u5c4f\u5e55\u4e0a\u6240\u770b\u5230\u7684\u5177\u4f53\u5b57\u7b26\uff08\u662fA\u8fd8\u662fB\uff0c\u662fA\u8fd8\u662fa\u7b49\uff09\uff0cAndroid12\u9ed8\u8ba4\u7684\u5b57\u7b26\u6620\u5c04\u914d\u7f6e\u4f4d\u4e8e\/system\/usr\/keychars\u5185\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">trout_x86:\/system\/usr\/keychars $ \ntrout_x86:\/system\/usr\/keychars $ ls -la\ntotal 88\ndrwxr-xr-x 2 root root  4096 2009-01-01 08:00 .\ndrwxr-xr-x 7 root root  4096 2009-01-01 08:00 ..\n-rw-r--r-- 1 root root 15891 2009-01-01 08:00 Generic.kcm\n-rw-r--r-- 1 root root  1234 2009-01-01 08:00 Vendor_18d1_Product_0200.kcm\n-rw-r--r-- 1 root root  8318 2009-01-01 08:00 Vendor_18d1_Product_5018.kcm\n-rw-r--r-- 1 root root 15666 2009-01-01 08:00 Virtual.kcm\n-rw-r--r-- 1 root root 15840 2009-01-01 08:00 qwerty.kcm\n-rw-r--r-- 1 root root 15959 2009-01-01 08:00 qwerty2.kcm\ntrout_x86:\/system\/usr\/keychars $ <\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u5176\u4e2dGeneric.kcm\u7528\u4e8e\u6807\u51c6\u5916\u90e8\u952e\u76d8\u7684\u5b57\u7b26\u6620\u5c04\uff0cVirtual.kcm\u7528\u4e8e\u865a\u62df\u952e\u76d8\u7684\u5b57\u7b26\u6620\u5c04\u3002\u4ee5Vendor\u5f00\u5934\u7684\u5219\u5c5e\u4e8e\u786c\u4ef6\u5382\u5546\u6240\u751f\u4ea7\u7684\u952e\u76d8\u5b57\u7b26\u6620\u5c04\u3002\u6211\u4eec\u53ef\u4ee5\u6253\u5f00\u770b\u770b\u6587\u4ef6\u5185\u7684\u5177\u4f53\u5185\u5bb9\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\"># Copyright (C) 2010 The Android Open Source Project\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#      http:\/\/www.apache.org\/licenses\/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\n#\n# Key character map for a built-in generic virtual keyboard primarily used\n# for instrumentation and testing purposes.\n#\n\ntype FULL\n\n### Basic QWERTY keys ###\n\nkey A {\n    label:                              'A'\n    base:                               'a'\n    shift, capslock:                    'A'\n    shift+capslock:                     'a'\n}\n\nkey B {\n    label:                              'B'\n    base:                               'b'\n    shift, capslock:                    'B'\n    shift+capslock:                     'b'\n}\n\nkey C {\n    label:                              'C'\n    base:                               'c'\n    shift, capslock:                    'C'\n    alt:                                '\\u00e7'\n    shift+alt:                          '\\u00c7'\n    shift+capslock:                     'c'\n}<\/code><\/pre>\n\n\n\n<p>\u66f4\u591a\u4fe1\u606f\u53ef\u53c2\u8003<a href=\"https:\/\/source.android.com\/docs\/core\/interaction\/input\/key-character-map-files?hl=zh-cn\" class=\"ek-link\">Link<\/a>\u3002<\/p>\n\n\n\n<p class=\"has-text-align-justify\">*.idc:\u8f93\u5165\u8bbe\u5907\u914d\u7f6e\u6587\u4ef6\uff0c\u5305\u542b\u8bbe\u5907\u4e13\u7528\u7684\u5c5e\u6027\u914d\u7f6e\uff0c\u8fd9\u4e9b\u901a\u5e38\u662f\u4e00\u4e9b\u8f93\u5165\u8bbe\u5907\u6240\u7279\u6709\u7684\u3001\u975e\u901a\u7528\u7684\u914d\u7f6e\uff0cAndroid12\u4e2d\u6211\u4eec\u53ef\u4ee5\u5728\/system\/usr\/idc\u76ee\u5f55\u4e0b\u627e\u5230\u76f8\u5173\u914d\u7f6e\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">trout_x86:\/system\/usr\/idc $ ls -la\ntotal 36\ndrwxr-xr-x 2 root root 4096 2009-01-01 08:00 .\ndrwxr-xr-x 7 root root 4096 2009-01-01 08:00 ..\n-rw-r--r-- 1 root root  634 2009-01-01 08:00 AVRCP.idc\n-rw-r--r-- 1 root root 1396 2009-01-01 08:00 Vendor_054c_Product_05c4.idc\n-rw-r--r-- 1 root root 1396 2009-01-01 08:00 Vendor_054c_Product_09cc.idc\n-rw-r--r-- 1 root root  782 2009-01-01 08:00 Vendor_0957_Product_0001.idc\n-rw-r--r-- 1 root root  916 2009-01-01 08:00 Vendor_248a_Product_8266.idc\n-rw-r--r-- 1 root root  869 2009-01-01 08:00 qwerty.idc\n-rw-r--r-- 1 root root  870 2009-01-01 08:00 qwerty2.idc\ntrout_x86:\/system\/usr\/idc $ <\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u4ee5\u4e0a\u662f\u76d1\u542c\u8bbe\u5907\u6302\u8f7d\u3001\u52a0\u8f7d\u8bbe\u5907\u914d\u7f6e\u4e0e\u5185\u90e8\u5b9e\u4f8b\u5316\u7684\u8fc7\u7a0b\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u770b\u770b\u4e8b\u4ef6\u8bfb\u53d6\u7684\u5177\u4f53\u8fc7\u7a0b\u3002\u4e8b\u4ef6\u7684\u8bfb\u53d6\u5165\u53e3\u4f4d\u4e8eEventhub.cpp\u7684getEvents\u51fd\u6570\uff0c\u8fd9\u4e5f\u662fEventhub\u4e2d\u6700\u4e3a\u6838\u5fc3\u6700\u4e3a\u91cd\u8981\u7684\u51fd\u6570\uff0c\u9274\u4e8e\u8be5\u51fd\u6570\u6bd4\u8f83\u590d\u6742\uff0c\u6211\u4eec\u5c06\u5176\u5206\u4e3a\u51e0\u90e8\u5206\u9010\u4e00\u8fdb\u884c\u5206\u6790\u3002<\/p>\n\n\n\n<p class=\"has-text-align-justify\">1.\u9996\u5148\u662f\u5224\u65ad\u662f\u5426\u5b58\u5728\u8bbe\u5907\u6dfb\u52a0\u3001\u79fb\u9664\u7b49\u4e8b\u4ef6\uff0c\u5982\u5b58\u5728\uff0c\u5219\u5c06\u8fd9\u4e9b\u4e8b\u4ef6\u4e0a\u62a5\u81f3InputReader\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"generic\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize){\n    .....\n    \/\/ \u5b58\u5728\u8bbe\u5907\u88ab\u5173\u95ed\uff0c\u8fdb\u884c\u4e0a\u62a5\uff0c event->type = DEVICE_REMOVED\n    for (auto it = mClosingDevices.begin(); it != mClosingDevices.end();)\n    {\n        std::unique_ptr&lt;Device> device = std::move(*it);\n        ALOGV(\"Reporting device closed: id=%d, name=%s\\n\", device->id, device->path.c_str());\n        event->when = now;\n        event->deviceId = (device->id == mBuiltInKeyboardId) ? ReservedInputDeviceId::BUILT_IN_KEYBOARD_ID : device->id;\n        event->type = DEVICE_REMOVED;\n        event += 1;\n        it = mClosingDevices.erase(it);\n        mNeedToSendFinishedDeviceScan = true;\n        if (--capacity == 0)\n        {\n                break;\n         }\n        }\n        if (mNeedToScanDevices)\n        {\n            mNeedToScanDevices = false;\n            scanDevicesLocked();\n            mNeedToSendFinishedDeviceScan = true;\n        }\n        \/\/\u5b58\u5728\u8bbe\u5907\u88ab\u6253\u5f00\uff0c\u8fdb\u884c\u4e0a\u62a5\uff0cevent->type = DEVICE_ADDED;\n        while (!mOpeningDevices.empty())\n        {\n            std::unique_ptr&lt;Device> device = std::move(*mOpeningDevices.rbegin());\n            mOpeningDevices.pop_back();\n            ALOGV(\"Reporting device opened: id=%d, name=%s\\n\", device->id, device->path.c_str());\n            event->when = now;\n            event->deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;\n            event->type = DEVICE_ADDED;\n            event += 1;\n            \/\/ Try to find a matching video device by comparing device names\n            for (auto it = mUnattachedVideoDevices.begin(); it != mUnattachedVideoDevices.end();\n                 it++)\n            {\n                std::unique_ptr&lt;TouchVideoDevice>&amp; videoDevice = *it;\n                if (tryAddVideoDeviceLocked(*device, videoDevice))\n                {\n                    \/\/ videoDevice was transferred to 'device'\n                    it = mUnattachedVideoDevices.erase(it);\n                    break;\n                }\n            }\n            auto [dev_it, inserted] = mDevices.insert_or_assign(device->id, std::move(device));\n            if (!inserted)\n            {\n                ALOGW(\"Device id %d exists, replaced.\", device->id);\n            }\n            mNeedToSendFinishedDeviceScan = true;\n            if (--capacity == 0)\n            {\n                break;\n            }\n        }\n        \/\/\u8bbe\u5907\u626b\u63cf\u7ed3\u675f\uff0c\u8fdb\u884c\u4e0a\u62a5\uff0cevent->type = FINISHED_DEVICE_SCAN;\n        if (mNeedToSendFinishedDeviceScan)\n        {\n            mNeedToSendFinishedDeviceScan = false;\n            event->when = now;\n            event->type = FINISHED_DEVICE_SCAN;\n            event += 1;\n            if (--capacity == 0)\n            {\n                break;\n            }\n        }\n        .....\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u8fd9\u90e8\u5206\u903b\u8f91\u662f\u56e0\u4e3a\u6211\u4eec\u5728\u8bfb\u53d6Event\u4e8b\u4ef6\u7684\u65f6\u5019\u968f\u65f6\u4f1a\u53d1\u751f\u8bbe\u5907\u5173\u95ed\u3001\u62d4\u6389\u3001\u51fa\u73b0\u5f02\u5e38\u6216\u8005\u65b0\u589e\u8bbe\u5907\u7b49\u60c5\u51b5\uff0c\u6240\u4ee5\u5f53\u5b58\u5728\u8fd9\u4e9b\u60c5\u51b5\u65f6\u6211\u4eec\u9700\u8981\u4f18\u5148\u8fdb\u884c\u5904\u7406\uff0c\u53ca\u65f6\u66f4\u65b0\u8bbe\u5907\u5217\u8868\uff0c\u624d\u80fd\u4fdd\u969c\u540e\u7eed\u7684Input event\u8bfb\u53d6\u3002<\/p>\n\n\n\n<p class=\"has-text-align-justify\">2.\u6b63\u5e38\u8bfb\u53d6\u5185\u6838\u4e0a\u62a5\u7684Input\u4e8b\u4ef6<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"cpp\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">   if (eventItem.events &amp; EPOLLIN) {\n....\n       const int32_t deviceId = device->id == mBuiltInKeyboardId ? 0 : device->id;\n       const size_t count = size_t(readSize) \/ sizeof(struct input_event);\n       for (size_t i = 0; i &lt; count; i++) {\n                        struct input_event&amp; iev = readBuffer[i];\n                        device->trackInputEvent(iev);\n                        events.push_back({\n                                .when = processEventTimestamp(iev),\n                                .readTime = systemTime(SYSTEM_TIME_MONOTONIC),\n                                .deviceId = deviceId,\n                                .type = iev.type,\n                                .code = iev.code,\n                                .value = iev.value,\n                        });\n                    }\n                    if (events.size() >= EVENT_BUFFER_SIZE) {\n                        \/\/ The result buffer is full.  Reset the pending event index\n                        \/\/ so we will try to read the device again on the next iteration.\n                        mPendingEventIndex -= 1;\n                        break;\n                    }\n}\n.....<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u8fd9\u91cc\u4f1a\u5c06\u4ece\u5185\u6838\u4e2d\u8bfb\u53d6\u5230\u7684\u4e8b\u4ef6\u5b58\u50a8\u5728Vector\u5185\uff0c\u5e76\u8fd4\u56de\u8be5Vector\uff0c\u8ddf\u968f\u7740\u8c03\u7528\u94fe\u8def\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u8be5\u65b9\u6cd5\u5728InputReader\u7c7b\u4e2d\u7684loopOnce\u65b9\u6cd5\u4e2d\u88ab\u8c03\u7528\uff0c\u83b7\u53d6\u5230\u8fd9\u4e9b\u8f93\u5165\u4e8b\u4ef6\u540e\uff0c\u8fdb\u5165processEventsLocked\u65b9\u6cd5\u5185\u3002<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"cpp\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">void InputReader::loopOnce() {\n...\nstd::vector&lt;RawEvent> events = mEventHub->getEvents(timeoutMillis);\n\n  { \/\/ acquire lock\n        std::scoped_lock _l(mLock);\n        mReaderIsAliveCondition.notify_all();\n\n        if (!events.empty()) {\n            mPendingArgs += processEventsLocked(events.data(), events.size());\n        }\n  }\n...\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u5728\u540e\u7eed\u7684\u5904\u7406\u4e2d\uff0c\u4f1a\u8df3\u8f6c\u5230InputDevice\u7c7b\u4e2d\u7684process\u65b9\u6cd5\uff0c\u5728\u8be5\u65b9\u6cd5\u4e2d\u4f1a\u901a\u8fc7InputMapper\u4f20\u9012\u5230\u5404\u4e2a\u5b50Mapper\u4e2d\uff0c\u8fd9\u91cc\u7684\u6bcf\u4e00\u4e2a\u5b50Mapper\u90fd\u5bf9\u5e94\u4e00\u4e2a\u7279\u5b9a\u7684\u8f93\u5165\u8bbe\u5907\uff0c\u5982\u952e\u76d8Keyboard\uff08KeyboardInputMapper\uff09\u3001\u64cd\u7eb5\u6746Joystick(JoystickInputMapper)\u3001Cursor(CursorInputMapper)\u7b49,\u4ee5\u952e\u76d8\u8f93\u5165\u4e3a\u4f8b\uff0c\u6211\u4eec\u6765\u770b\u770b\u9488\u5bf9\u952e\u76d8\u7684\u8f93\u5165\u5177\u4f53\u662f\u5982\u4f55\u5904\u7406\u7684\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"cpp\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">\/\/frameworks\/native\/services\/inputflinger\/reader\/mapper\/KeyboardInputMapper.cpp\nstd::list&lt;NotifyArgs> KeyboardInputMapper::process(const RawEvent&amp; rawEvent) {\n...\n           if (isSupportedScanCode(scanCode)) {\n                out += processKey(rawEvent.when, rawEvent.readTime, rawEvent.value != 0, scanCode,\n                                  mHidUsageAccumulator.consumeCurrentHidUsage());\n            }\n....\n}\n\n\/\/<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u5b9e\u9645\u4e0a\u6bcf\u4e2a\u5b50Mapper\u90fd\u4f1a\u4f9d\u7167\u8f93\u5165\u8bbe\u5907\u7684\u7279\u6027\u5bf9\u539f\u59cb\u7684Events\u6539\u5199\u4e00\u4e9b\u6570\u636e\uff0c\u540e\u7eed\u4f1a\u901a\u8fc7notify\u65b9\u6cd5\u5c06\u6539\u5199\u540e\u7684\u6570\u636e\u4f20\u9012\u5230InputDispatcher\u4e2d\uff0c\u8fd9\u4e00\u6b65\u53c8\u662f\u5982\u4f55\u5b9e\u73b0\u7684\u5462\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4e8b\u4ef6\u5206\u53d1-InputDispatcher<\/h4>\n\n\n\n<p class=\"has-text-align-justify\">\u9996\u5148\u6211\u4eec\u6765\u770b\u770b\u4e8b\u4ef6\u662f\u5982\u4f55\u4eceInputReader\u4f20\u9012\u5230InputDispatcher\u4e2d\u7684\uff0c\u5176\u5b9e\u5c31\u662f\u5728\u521b\u5efaInputReader\u65f6\u5c06InputDispatcher\u7684Listener\u4f20\u5165\uff0c\u5f53\u63a5\u6536\u5230Input\u4e8b\u4ef6\u65f6\u5373\u901a\u8fc7Listener\u8fdb\u884c\u540e\u7eed\u5904\u7406\uff0c\u5177\u4f53\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"cpp\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">InputManager::InputManager(const sp&lt;InputReaderPolicyInterface>&amp; readerPolicy,\n                           InputDispatcherPolicyInterface&amp; dispatcherPolicy,\n                           PointerChoreographerPolicyInterface&amp; choreographerPolicy,\n                           InputFilterPolicyInterface&amp; inputFilterPolicy) {\n\n    mDispatcher = createInputDispatcher(dispatcherPolicy);\n    mTracingStages.emplace_back(\n            std::make_unique&lt;TracedInputListener>(\"InputDispatcher\", *mDispatcher));\n    .....\n    mReader = createInputReader(readerPolicy, *mTracingStages.back());\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u8fd9\u91ccTracedInputListener\u7ee7\u627f\u4e8eInputListenerInterface\uff0cInputListenerInterface\u7528\u4e8eInputReader\u4f20\u9012\u4e8b\u4ef6\u81f3InputDispatcher\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"cpp\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">\/*\n * The interface used by the InputReader to notify the InputListener about input events.\n *\/\nclass InputListenerInterface {\npublic:\n    InputListenerInterface() { }\n    InputListenerInterface(const InputListenerInterface&amp;) = delete;\n    InputListenerInterface&amp; operator=(const InputListenerInterface&amp;) = delete;\n    virtual ~InputListenerInterface() { }\n\n    virtual void notifyInputDevicesChanged(const NotifyInputDevicesChangedArgs&amp; args) = 0;\n    virtual void notifyKey(const NotifyKeyArgs&amp; args) = 0;\n    virtual void notifyMotion(const NotifyMotionArgs&amp; args) = 0;\n    virtual void notifySwitch(const NotifySwitchArgs&amp; args) = 0;\n    virtual void notifySensor(const NotifySensorArgs&amp; args) = 0;\n    virtual void notifyVibratorState(const NotifyVibratorStateArgs&amp; args) = 0;\n    virtual void notifyDeviceReset(const NotifyDeviceResetArgs&amp; args) = 0;\n    virtual void notifyPointerCaptureChanged(const NotifyPointerCaptureChangedArgs&amp; args) = 0;\n\n    void notify(const NotifyArgs&amp; args);\n};<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u90a3\u4e48\u5177\u4f53\u662f\u5728\u4ec0\u4e48\u5730\u65b9\u8fdb\u884c\u4f20\u9012\u7684\u5462\uff0c\u8fd9\u91cc\u6211\u4eec\u56de\u5230InputReader\u7c7b\u7684loopOnce\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"cpp\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">void InputReader::loopOnce() {\n    .....\n    \/\/ Flush queued events out to the listener.\n    \/\/ This must happen outside of the lock because the listener could potentially call\n    \/\/ back into the InputReader's methods, such as getScanCodeState, or become blocked\n    \/\/ on another thread similarly waiting to acquire the InputReader lock thereby\n    \/\/ resulting in a deadlock.  This situation is actually quite plausible because the\n    \/\/ listener is actually the input dispatcher, which calls into the window manager,\n    \/\/ which occasionally calls into the input reader.\n    for (const NotifyArgs&amp; args : notifyArgs) {\n        mNextListener.notify(args);\n    }\n    ....\n\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u8fd9\u91cc\u7684mNextListener\u4e5f\u5c31\u662fInputDispatcher\u6ce8\u518c\u8fdb\u6765\u7684TracedInputListener\u5bf9\u8c61\uff0c\u901a\u8fc7notify\u65b9\u6cd5\u8fdb\u884cEvent\u5206\u53d1\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"cpp\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">void InputListenerInterface::notify(const NotifyArgs&amp; generalArgs) {\n    Visitor v{\n            [&amp;](const NotifyInputDevicesChangedArgs&amp; args) { notifyInputDevicesChanged(args); },\n            [&amp;](const NotifyKeyArgs&amp; args) { notifyKey(args); },\n            [&amp;](const NotifyMotionArgs&amp; args) { notifyMotion(args); },\n            [&amp;](const NotifySwitchArgs&amp; args) { notifySwitch(args); },\n            [&amp;](const NotifySensorArgs&amp; args) { notifySensor(args); },\n            [&amp;](const NotifyVibratorStateArgs&amp; args) { notifyVibratorState(args); },\n            [&amp;](const NotifyDeviceResetArgs&amp; args) { notifyDeviceReset(args); },\n            [&amp;](const NotifyPointerCaptureChangedArgs&amp; args) { notifyPointerCaptureChanged(args); },\n    };\n    std::visit(v, generalArgs);\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u8fd9\u91cc\u5b9e\u9645\u4f1a\u6839\u636eEvent\u7684\u7c7b\u578b\u4e0d\u540c\u8fdb\u5165\u5230\u4e0d\u540c\u7684notify\u5b50\u65b9\u6cd5\u4e2d\uff0c\u4ee5Key\u4e8b\u4ef6\u4e3a\u4f8b\u5b50\uff0c\u6700\u7ec8\u6211\u4eec\u8fdb\u5165\u5230InputDispatcher\u4e2d\u7684notifyKey\u65b9\u6cd5\u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"php\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">void InputDispatcher::notifyKey(const NotifyKeyArgs&amp; args) {\n...\n  needWake = enqueueInboundEventLocked(std::move(newEntry));\n....\n\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u8fd9\u91cc\u4f1a\u5c06Input\u4e8b\u4ef6\u585e\u5165\u961f\u5217\uff0c\u4e4b\u540e\u5524\u9192looper,\u901a\u8fc7dispatchOnce\u8fdb\u884c\u5206\u53d1\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"cpp\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">void InputDispatcher::dispatchOnce() {\n...\n        \/\/ Run a dispatch loop if there are no pending commands.\n        \/\/ The dispatch loop might enqueue commands to run afterwards.\n        if (!haveCommandsLocked()) {\n            dispatchOnceInnerLocked(\/*byref*\/ nextWakeupTime);\n        }\n....\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u4e4b\u540e\u4f1a\u8fdb\u5165dispatchOnceInnerLocked\u65b9\u6cd5\uff0c\u5728\u8fd9\u5176\u4e2d\u4f1a\u6839\u636eEvent\u7c7b\u578b\u4e0d\u540c\u8fdb\u884c\u5206\u53d1\uff0c\u8fd9\u91cc\u7684Event\u7c7b\u578b\u5305\u542bDEVICE_RESET\u3001FOCUS\u3001TOUCH_MODE_CHANGED\u3001POINTER_CAPTURE_CHANGED\u3001KEY\u7b49\u7b49\uff0c\u8fd9\u91cc\u6211\u4eec\u8fd8\u662f\u4ee5Key\u7ee7\u7eed\u5f80\u4e0b\u770b\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"cpp\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">case EventEntry::Type::KEY: {\n....\n      std::shared_ptr&lt;const KeyEntry> keyEntry =\n                    std::static_pointer_cast&lt;const KeyEntry>(mPendingEvent);\n      if (dropReason == DropReason::NOT_DROPPED &amp;&amp; isStaleEvent(currentTime, *keyEntry)) {\n                dropReason = DropReason::STALE;\n      }\n      if (dropReason == DropReason::NOT_DROPPED &amp;&amp; mNextUnblockedEvent) {\n                dropReason = DropReason::BLOCKED;\n      }\n      done = dispatchKeyLocked(currentTime, keyEntry, &amp;dropReason, nextWakeupTime);\n      break;\n....\n}\n<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u5728\u4e8b\u4ef6\u5206\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u9700\u8981\u5bf9\u5e94\u5230\u5177\u4f53\u7684\u7a97\u53e3\uff0c\u4ee5\u4fbf\u62e5\u6709\u8be5\u7a97\u53e3\u7684\u5e94\u7528\u8fdb\u884c\u540e\u7eed\u5904\u7406\u3002\u8fd9\u4e00\u6b65\u5176\u5b9e\u5c31\u662f\u5728dispatchKeyLocked\u4e2d\u8fdb\u884c\u7684\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"php\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">bool InputDispatcher::dispatchKeyLocked(nsecs_t currentTime, std::shared_ptr&lt;const KeyEntry> entry,\n                                        DropReason* dropReason, nsecs_t&amp; nextWakeupTime) {\n\n  \/\/ Identify targets.\n    Result&lt;sp&lt;WindowInfoHandle>, InputEventInjectionResult> result =\n            findFocusedWindowTargetLocked(currentTime, *entry, nextWakeupTime);\n\n    if (!result.ok()) {\n        if (result.error().code() == InputEventInjectionResult::PENDING) {\n            return false;\n        }\n        setInjectionResult(*entry, result.error().code());\n        return true;\n    }\n    sp&lt;WindowInfoHandle>&amp; focusedWindow = *result;\n    LOG_ALWAYS_FATAL_IF(focusedWindow == nullptr);\n\n    setInjectionResult(*entry, InputEventInjectionResult::SUCCEEDED);\n\n    std::vector&lt;InputTarget> inputTargets;\n    addWindowTargetLocked(focusedWindow, InputTarget::DispatchMode::AS_IS,\n                          InputTarget::Flags::FOREGROUND, getDownTime(*entry), inputTargets);\n\n    \/\/ Add monitor channels from event's or focused display.\n    addGlobalMonitoringTargetsLocked(inputTargets, getTargetDisplayId(*entry));\n\n    if (mTracer) {\n        ensureEventTraced(*entry);\n        for (const auto&amp; target : inputTargets) {\n            mTracer->dispatchToTargetHint(*entry->traceTracker, target);\n        }\n    }\n\n    \/\/ Dispatch the key.\n    dispatchEventLocked(currentTime, entry, inputTargets);\n    return true;\n\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u6240\u6709\u7684\u6b65\u9aa4\u5b8c\u6210\u540e\uff0c\u5199\u5165\u5bf9\u5e94\u7684Channel\uff0c\u5e94\u7528\u5c31\u80fd\u63a5\u6536\u5230\u8fd9\u4e9bInput\u4e8b\u4ef6\u8fdb\u884c\u540e\u7eed\u5904\u7406\u4e86\u3002\u4ee5\u6309\u952e\u4e8b\u4ef6\u4e3a\u4f8b\uff0c\u8fd9\u91cc\u6211\u4eec\u8df3\u8f6c\u5230InputTransport\u7c7b\u7684publishKeyEvent\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"io-enlighter-pre\"><code class=\"gl\" data-enlighter-language=\"cpp\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"\" data-enlighter-highlight=\"\">status_t InputPublisher::publishKeyEvent(uint32_t seq, int32_t eventId, int32_t deviceId,\n                                         int32_t source, ui::LogicalDisplayId displayId,\n                                         std::array&lt;uint8_t, 32> hmac, int32_t action,\n                                         int32_t flags, int32_t keyCode, int32_t scanCode,\n                                         int32_t metaState, int32_t repeatCount, nsecs_t downTime,\n                                         nsecs_t eventTime) {\n....\nreturn mChannel->sendMessage(&amp;msg);\n}<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-justify\">\u4ee5\u4e0a\u5c31\u662f\u6574\u4e2a\u4e8b\u4ef6\u8bfb\u53d6\u4e0e\u5206\u53d1\u7684\u5168\u8fc7\u7a0b\u4e86\uff0c\u6700\u540e\u6211\u4eec\u4ee5\u4e0b\u56fe\u6765\u4f5c\u4e3a\u603b\u7ed3\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"988\" height=\"689\" src=\"https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/05\/image-1.png\" alt=\"\" class=\"wp-image-4689\" srcset=\"https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/05\/image-1.png 988w, https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/05\/image-1-300x209.png 300w, https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/05\/image-1-768x536.png 768w, https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/05\/image-1-18x12.png 18w\" sizes=\"auto, (max-width: 988px) 100vw, 988px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-justify\">\u7531\u4e8e\u7bc7\u5e45\u6709\u9650\uff0c\u8fd9\u7bc7\u6587\u7ae0\u5e76\u672a\u89e3\u6790App\u662f\u5982\u4f55\u63a5\u6536\u4e8b\u4ef6\u5e76\u8fdb\u884c\u5904\u7406\u7684\uff0c\u540e\u9762\u6709\u673a\u4f1a\u53ef\u4ee5\u53e6\u5916\u5199\u4e00\u7bc7\u6587\u7ae0\u8fdb\u884c\u5206\u6790\u3002<\/p>\n        <div class=\"booster-block booster-reactions-block\">\n            <div class=\"twp-reactions-icons\">\n                \n                <div class=\"twp-reacts-wrap\">\n                    <a react-data=\"be-react-1\" post-id=\"4333\" class=\"be-face-icons un-reacted\" href=\"javascript:void(0)\">\n                        <img decoding=\"async\" src=\"https:\/\/blog.coderfan.org\/wp-content\/plugins\/booster-extension\/\/assets\/icon\/happy.svg\" alt=\"Happy\">\n                    <\/a>\n                    <div class=\"twp-reaction-title\">\n                        Happy                    <\/div>\n                    <div class=\"twp-count-percent\">\n                                                    <span style=\"display: none;\" class=\"twp-react-count\">0<\/span>\n                        \n                                                <span class=\"twp-react-percent\"><span>0<\/span> %<\/span>\n                                            <\/div>\n                <\/div>\n\n                <div class=\"twp-reacts-wrap\">\n                    <a react-data=\"be-react-2\" post-id=\"4333\" class=\"be-face-icons un-reacted\" href=\"javascript:void(0)\">\n                        <img decoding=\"async\" src=\"https:\/\/blog.coderfan.org\/wp-content\/plugins\/booster-extension\/\/assets\/icon\/sad.svg\" alt=\"Sad\">\n                    <\/a>\n                    <div class=\"twp-reaction-title\">\n                        Sad                    <\/div>\n                    <div class=\"twp-count-percent\">\n                                                    <span style=\"display: none;\" class=\"twp-react-count\">0<\/span>\n                                                                        <span class=\"twp-react-percent\"><span>0<\/span> %<\/span>\n                                            <\/div>\n                <\/div>\n\n                <div class=\"twp-reacts-wrap\">\n                    <a react-data=\"be-react-3\" post-id=\"4333\" class=\"be-face-icons un-reacted\" href=\"javascript:void(0)\">\n                        <img decoding=\"async\" src=\"https:\/\/blog.coderfan.org\/wp-content\/plugins\/booster-extension\/\/assets\/icon\/excited.svg\" alt=\"Excited\">\n                    <\/a>\n                    <div class=\"twp-reaction-title\">\n                        Excited                    <\/div>\n                    <div class=\"twp-count-percent\">\n                                                    <span style=\"display: none;\" class=\"twp-react-count\">0<\/span>\n                                                                        <span class=\"twp-react-percent\"><span>0<\/span> %<\/span>\n                                            <\/div>\n                <\/div>\n\n                <div class=\"twp-reacts-wrap\">\n                    <a react-data=\"be-react-6\" post-id=\"4333\" class=\"be-face-icons un-reacted\" href=\"javascript:void(0)\">\n                        <img decoding=\"async\" src=\"https:\/\/blog.coderfan.org\/wp-content\/plugins\/booster-extension\/\/assets\/icon\/sleepy.svg\" alt=\"Sleepy\">\n                    <\/a>\n                    <div class=\"twp-reaction-title\">\n                        Sleepy                    <\/div>\n                    <div class=\"twp-count-percent\">\n                                                    <span style=\"display: none;\" class=\"twp-react-count\">0<\/span>\n                        \n                                                <span class=\"twp-react-percent\"><span>0<\/span> %<\/span>\n                                            <\/div>\n                <\/div>\n\n                <div class=\"twp-reacts-wrap\">\n                    <a react-data=\"be-react-4\" post-id=\"4333\" class=\"be-face-icons un-reacted\" href=\"javascript:void(0)\">\n                        <img decoding=\"async\" src=\"https:\/\/blog.coderfan.org\/wp-content\/plugins\/booster-extension\/\/assets\/icon\/angry.svg\" alt=\"Angry\">\n                    <\/a>\n                    <div class=\"twp-reaction-title\">Angry<\/div>\n                    <div class=\"twp-count-percent\">\n                                                    <span style=\"display: none;\" class=\"twp-react-count\">0<\/span>\n                                                                        <span class=\"twp-react-percent\"><span>0<\/span> %<\/span>\n                        \n                    <\/div>\n                <\/div>\n\n                <div class=\"twp-reacts-wrap\">\n                    <a react-data=\"be-react-5\" post-id=\"4333\" class=\"be-face-icons un-reacted\" href=\"javascript:void(0)\">\n                        <img decoding=\"async\" src=\"https:\/\/blog.coderfan.org\/wp-content\/plugins\/booster-extension\/\/assets\/icon\/surprise.svg\" alt=\"Surprise\">\n                    <\/a>\n                    <div class=\"twp-reaction-title\">Surprise<\/div>\n                    <div class=\"twp-count-percent\">\n                                                    <span style=\"display: none;\" class=\"twp-react-count\">0<\/span>\n                                                                        <span class=\"twp-react-percent\"><span>0<\/span> %<\/span>\n                                            <\/div>\n                <\/div>\n\n            <\/div>\n        <\/div>","protected":false},"excerpt":{"rendered":"<p>\u5b89\u5353\u7cfb\u7edfInput\u8bfb\u53d6\u4e0e\u5206\u53d1\u6d41\u7a0b\u89e3\u6790\u3002<\/p>","protected":false},"author":1,"featured_media":4483,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_editorskit_title_hidden":false,"_editorskit_reading_time":7,"_editorskit_is_block_options_detached":false,"_editorskit_block_options_position":"{}","om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[18],"tags":[46,434,33],"class_list":["post-4333","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-operating-system","tag-android","tag-input","tag-linux"],"blocksy_meta":[],"aioseo_notices":[],"featured_image_urls":{"full":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android.jpg",512,269,false],"thumbnail":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android-150x150.jpg",150,150,true],"medium":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android-300x158.jpg",300,158,true],"medium_large":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android.jpg",512,269,false],"large":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android.jpg",512,269,false],"1536x1536":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android.jpg",512,269,false],"2048x2048":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android.jpg",512,269,false],"trp-custom-language-flag":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android-18x9.jpg",18,9,true]},"post_excerpt_stackable":"<p>\u5b89\u5353\u7cfb\u7edfInput\u8bfb\u53d6\u4e0e\u5206\u53d1\u6d41\u7a0b\u89e3\u6790\u3002<\/p>\n","category_list":"<a href=\"https:\/\/blog.coderfan.org\/en\/category\/operating-system\" rel=\"category tag\">\u64cd\u4f5c\u7cfb\u7edf<\/a>","author_info":{"name":"FranzKafka95","url":"https:\/\/blog.coderfan.org\/en\/author\/yushenglonely95"},"comments_num":"0 comments","featured_image_urls_v2":{"full":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android.jpg",512,269,false],"thumbnail":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android-150x150.jpg",150,150,true],"medium":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android-300x158.jpg",300,158,true],"medium_large":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android.jpg",512,269,false],"large":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android.jpg",512,269,false],"1536x1536":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android.jpg",512,269,false],"2048x2048":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android.jpg",512,269,false],"trp-custom-language-flag":["https:\/\/blog.coderfan.org\/wp-content\/uploads\/2023\/04\/android-18x9.jpg",18,9,true]},"post_excerpt_stackable_v2":"<p>\u5b89\u5353\u7cfb\u7edfInput\u8bfb\u53d6\u4e0e\u5206\u53d1\u6d41\u7a0b\u89e3\u6790\u3002<\/p>\n","category_list_v2":"<a href=\"https:\/\/blog.coderfan.org\/en\/category\/operating-system\" rel=\"category tag\">\u64cd\u4f5c\u7cfb\u7edf<\/a>","author_info_v2":{"name":"FranzKafka95","url":"https:\/\/blog.coderfan.org\/en\/author\/yushenglonely95"},"comments_num_v2":"0 comments","_links":{"self":[{"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/posts\/4333","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/comments?post=4333"}],"version-history":[{"count":35,"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/posts\/4333\/revisions"}],"predecessor-version":[{"id":5381,"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/posts\/4333\/revisions\/5381"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/media\/4483"}],"wp:attachment":[{"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/media?parent=4333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/categories?post=4333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coderfan.org\/en\/wp-json\/wp\/v2\/tags?post=4333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}