自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
按键输入
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
Android Automotive 根据 hardware/libhardware/include/hardware/vehicle.h
中定义的车载 HAL 属性 VEHICLE_PROPERTY_HW_KEY_INPUT
处理来自转向远程开关、硬件按钮和触摸面板等元素的按键输入。
例如,通过 CAN 总线网络调度按键事件:

图 1. 通过 CAN 总线调度 Android Automotive 按键事件的流程。
按键事件数据
车载 HAL 中的每个按键事件都附加了以下数据:
- 输入操作(向上/向下):
VEHICLE_HW_KEY_INPUT_ACTION_DOWN = 0,
VEHICLE_HW_KEY_INPUT_ACTION_UP = 1,
- 键码(映射到车载 HAL 的合作伙伴实现中的 Android 按键)。
- 目标屏幕(主屏幕/仪表板):
VEHICLE_DISPLAY_MAIN = 0,
VEHICLE_DISPLAY_INSTRUMENT_CLUSTER = 1,
仪表板中的按键输入处理会使用 CarInputService
(车辆的常规输入处理也使用此服务)。按键事件将传播到 InstrumentClusterRenderingService
:
public class MyClusterRenderingService extends
InstrumentClusterRenderingService {
...
@Override protected void onKeyEvent(KeyEvent keyEvent) { /* Inject to
Presentation? */ }
}
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[null,null,["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Key input\n\nBased on the Vehicle HAL property `VEHICLE_PROPERTY_HW_KEY_INPUT`, defined in\n`hardware/libhardware/include/hardware/vehicle.h`, Android Automotive handles key input\nfrom elements that include steering remote switches, hardware buttons, and touch panels.\n\nFor example, dispatch of a key event over a CAN bus network:\n\n**Figure 1.**Android Automotive key events over CANbus flow.\n\nKey event data\n--------------\n\nThe following data is attached to each key event in the Vehicle HAL:\n\n- Input action (up/down): \n\n ```scdoc\n VEHICLE_HW_KEY_INPUT_ACTION_DOWN = 0,\n VEHICLE_HW_KEY_INPUT_ACTION_UP = 1,\n ```\n- Key code mapped to Android keys in a partner implementation of the Vehicle HAL.\n- Target display (main/instrument cluster): \n\n ```scdoc\n VEHICLE_DISPLAY_MAIN = 0,\n VEHICLE_DISPLAY_INSTRUMENT_CLUSTER = 1,\n ```\n\nThe key input handling in the instrument cluster uses `CarInputService` (the same\nservice uses for the regular input handling in the vehicle). Key events get propagated to the\n`InstrumentClusterRenderingService`: \n\n```transact-sql\npublic class MyClusterRenderingService extends\nInstrumentClusterRenderingService {\n ...\n @Override protected void onKeyEvent(KeyEvent keyEvent) { /* Inject to\n Presentation? */ }\n}\n```"]]