绑定基础输入事件
========

NDK接口提供了监听和处理基础输入事件的能力，可绑定点击事件（[NODE\_ON\_CLICK\_EVENT](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-node-h#arkui_nodeeventtype)）、触摸事件（[NODE\_TOUCH\_EVENT](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-node-h#arkui_nodeeventtype)）、鼠标事件（[NODE\_ON\_MOUSE](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-node-h#arkui_nodeeventtype)）、悬浮事件（[NODE\_ON\_HOVER\_EVENT](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-node-h#arkui_nodeeventtype)）、轴事件（[NODE\_ON\_AXIS](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-node-h#arkui_nodeeventtype)）、按键事件（[NODE\_ON\_KEY\_EVENT](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-node-h#arkui_nodeeventtype)），然后通过事件接收器中的回调函数获取事件的详细信息。

以下示例均需基于[接入ArkTS页面](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/ndk-access-the-arkts-page)章节，详细代码请参考[完整示例](#完整示例)。

#### 点击事件

点击事件在用户点击组件时触发，可获取点击位置、触摸点数量、指针ID、压力值等信息。可对节点绑定如下处理函数。

// 处理点击事件
void HandleClickEvent(ArkUI\_UIInputEvent \*inputEvent)
{
    auto pos = GetPointerPosition(inputEvent);
    auto baseInfo = GetBaseEventInfo(inputEvent);
    auto pointerCount = OH\_ArkUI\_PointerEvent\_GetPointerCount(inputEvent);
    auto pointerId = OH\_ArkUI\_PointerEvent\_GetPointerId(inputEvent, 0);
    auto pressure = OH\_ArkUI\_PointerEvent\_GetPressure(inputEvent, 0);

    // 通过索引获取指针位置信息（支持多点触控场景）
    auto xByIndex = OH\_ArkUI\_PointerEvent\_GetXByIndex(inputEvent, 0);
    auto yByIndex = OH\_ArkUI\_PointerEvent\_GetYByIndex(inputEvent, 0);
    auto displayXByIndex = OH\_ArkUI\_PointerEvent\_GetDisplayXByIndex(inputEvent, 0);
    auto displayYByIndex = OH\_ArkUI\_PointerEvent\_GetDisplayYByIndex(inputEvent, 0);
    auto windowXByIndex = OH\_ArkUI\_PointerEvent\_GetWindowXByIndex(inputEvent, 0);
    auto windowYByIndex = OH\_ArkUI\_PointerEvent\_GetWindowYByIndex(inputEvent, 0);

    OH\_LOG\_Print(LOG\_APP, LOG\_INFO, LOG\_PRINT\_DOMAIN, "\[NdkBindInputEvent\]",
        "\[%{public}s\] Click: x=%{public}f, y=%{public}f, displayX=%{public}f, displayY=%{public}f, "
        "windowX=%{public}f, windowY=%{public}f, xByIndex=%{public}f, yByIndex=%{public}f, "
        "displayXByIndex=%{public}f, displayYByIndex=%{public}f, windowXByIndex=%{public}f, "
        "windowYByIndex=%{public}f, pointerCount=%{public}d, pointerId=%{public}d, "
        "pressure=%{public}f, action=%{public}d, eventTime=%{public}lld, sourceType=%{public}d, type=%{public}d",
        eventName\_.c\_str(), pos.x, pos.y, pos.displayX, pos.displayY, pos.windowX, pos.windowY,
        xByIndex, yByIndex, displayXByIndex, displayYByIndex, windowXByIndex, windowYByIndex,
        pointerCount, pointerId, pressure, baseInfo.action, baseInfo.eventTime,
        baseInfo.sourceType, baseInfo.type);
}

从API version 18开始，新增[NODE\_ON\_CLICK\_EVENT](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-node-h#arkui_nodeeventtype)枚举。节点自身可以注册NODE\_ON\_CLICK\_EVENT事件监听，以响应上述处理函数。

nativeModule\_->registerNodeEvent(handle\_, NODE\_ON\_CLICK\_EVENT, 0, this);

#### 触摸事件

触摸事件在用户触摸屏幕时触发，除了点击事件的信息外，还可获取触摸区域宽度和高度。可对节点绑定如下处理函数。

// 处理触摸事件
void HandleTouchEvent(ArkUI\_UIInputEvent \*inputEvent)
{
    auto pos = GetPointerPosition(inputEvent);
    auto baseInfo = GetBaseEventInfo(inputEvent);
    auto pointerCount = OH\_ArkUI\_PointerEvent\_GetPointerCount(inputEvent);
    auto pointerId = OH\_ArkUI\_PointerEvent\_GetPointerId(inputEvent, 0);
    auto pressure = OH\_ArkUI\_PointerEvent\_GetPressure(inputEvent, 0);
    auto touchAreaWidth = OH\_ArkUI\_PointerEvent\_GetTouchAreaWidth(inputEvent, 0);
    auto touchAreaHeight = OH\_ArkUI\_PointerEvent\_GetTouchAreaHeight(inputEvent, 0);

    // 通过索引获取指针位置信息（支持多点触控场景）
    auto xByIndex = OH\_ArkUI\_PointerEvent\_GetXByIndex(inputEvent, 0);
    auto yByIndex = OH\_ArkUI\_PointerEvent\_GetYByIndex(inputEvent, 0);
    auto displayXByIndex = OH\_ArkUI\_PointerEvent\_GetDisplayXByIndex(inputEvent, 0);
    auto displayYByIndex = OH\_ArkUI\_PointerEvent\_GetDisplayYByIndex(inputEvent, 0);
    auto windowXByIndex = OH\_ArkUI\_PointerEvent\_GetWindowXByIndex(inputEvent, 0);
    auto windowYByIndex = OH\_ArkUI\_PointerEvent\_GetWindowYByIndex(inputEvent, 0);

    OH\_LOG\_Print(LOG\_APP, LOG\_INFO, LOG\_PRINT\_DOMAIN, "\[NdkBindInputEvent\]",
        "\[%{public}s\] Touch: x=%{public}f, y=%{public}f, displayX=%{public}f, displayY=%{public}f, "
        "windowX=%{public}f, windowY=%{public}f, xByIndex=%{public}f, yByIndex=%{public}f, "
        "displayXByIndex=%{public}f, displayYByIndex=%{public}f, windowXByIndex=%{public}f, "
        "windowYByIndex=%{public}f, pointerCount=%{public}d, pointerId=%{public}d, "
        "pressure=%{public}f, touchAreaWidth=%{public}f, touchAreaHeight=%{public}f, "
        "action=%{public}d, eventTime=%{public}lld, sourceType=%{public}d, type=%{public}d",
        eventName\_.c\_str(), pos.x, pos.y, pos.displayX, pos.displayY, pos.windowX, pos.windowY,
        xByIndex, yByIndex, displayXByIndex, displayYByIndex, windowXByIndex, windowYByIndex,
        pointerCount, pointerId, pressure, touchAreaWidth, touchAreaHeight, baseInfo.action,
        baseInfo.eventTime, baseInfo.sourceType, baseInfo.type);
}

节点自身需要注册[NODE\_TOUCH\_EVENT](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-node-h#arkui_nodeeventtype)事件监听，以响应上述处理函数。

nativeModule\_->registerNodeEvent(handle\_, NODE\_TOUCH\_EVENT, 0, this);

#### 鼠标事件

鼠标事件在使用鼠标操作时触发，可获取鼠标按键类型、鼠标动作类型、滚轮移动增量等信息。可对节点绑定如下处理函数。

// 处理鼠标事件
void HandleMouseEvent(ArkUI\_UIInputEvent \*inputEvent)
{
    auto pos = GetPointerPosition(inputEvent);
    auto baseInfo = GetBaseEventInfo(inputEvent);
    auto button = OH\_ArkUI\_MouseEvent\_GetMouseButton(inputEvent);
    auto mouseAction = OH\_ArkUI\_MouseEvent\_GetMouseAction(inputEvent);
    auto rawDeltaX = OH\_ArkUI\_MouseEvent\_GetRawDeltaX(inputEvent);
    auto rawDeltaY = OH\_ArkUI\_MouseEvent\_GetRawDeltaY(inputEvent);
    auto buttonCount = GetPressedButtonsCount(inputEvent);

    OH\_LOG\_Print(LOG\_APP, LOG\_INFO, LOG\_PRINT\_DOMAIN, "\[NdkBindInputEvent\]",
        "\[%{public}s\] Mouse: x=%{public}f, y=%{public}f, displayX=%{public}f, displayY=%{public}f, "
        "windowX=%{public}f, windowY=%{public}f, button=%{public}d, mouseAction=%{public}d, "
        "pressedButtonsCount=%{public}d, rawDeltaX=%{public}f, rawDeltaY=%{public}f, "
        "action=%{public}d, eventTime=%{public}lld, sourceType=%{public}d, type=%{public}d",
        eventName\_.c\_str(), pos.x, pos.y, pos.displayX, pos.displayY, pos.windowX, pos.windowY,
        button, mouseAction, buttonCount, rawDeltaX, rawDeltaY, baseInfo.action,
        baseInfo.eventTime, baseInfo.sourceType, baseInfo.type);
}

节点自身需要注册[NODE\_ON\_MOUSE](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-node-h#arkui_nodeeventtype)事件监听，以响应上述处理函数。

nativeModule\_->registerNodeEvent(handle\_, NODE\_ON\_MOUSE, 0, this);

#### 悬浮事件

悬浮事件在鼠标指针移至组件上方或远离组件时触发，可获取悬浮状态。可对节点绑定如下处理函数。

// 处理悬浮事件
void HandleHoverEvent(ArkUI\_UIInputEvent \*inputEvent)
{
    auto pos = GetPointerPosition(inputEvent);
    auto baseInfo = GetBaseEventInfo(inputEvent);
    auto isHovered = OH\_ArkUI\_HoverEvent\_IsHovered(inputEvent);

    OH\_LOG\_Print(LOG\_APP, LOG\_INFO, LOG\_PRINT\_DOMAIN, "\[NdkBindInputEvent\]",
        "\[%{public}s\] Hover: x=%{public}f, y=%{public}f, displayX=%{public}f, displayY=%{public}f, "
        "windowX=%{public}f, windowY=%{public}f, isHovered=%{public}d, action=%{public}d, "
        "eventTime=%{public}lld, sourceType=%{public}d, type=%{public}d",
        eventName\_.c\_str(), pos.x, pos.y, pos.displayX, pos.displayY, pos.windowX, pos.windowY,
        isHovered, baseInfo.action, baseInfo.eventTime, baseInfo.sourceType, baseInfo.type);
}

从API version 17开始，新增[NODE\_ON\_HOVER\_EVENT](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-node-h#arkui_nodeeventtype)枚举。节点自身可以注册NODE\_ON\_HOVER\_EVENT事件监听，以响应上述处理函数。

nativeModule\_->registerNodeEvent(handle\_, NODE\_ON\_HOVER\_EVENT, 0, this);

#### 轴事件

轴事件通过鼠标滚轮、游戏摇杆等设备触发，可获取轴动作类型、垂直轴值、水平轴值、缩放比例、滚动步长等信息。可对节点绑定如下处理函数。

// 处理轴事件
void HandleAxisEvent(ArkUI\_UIInputEvent \*inputEvent)
{
    auto baseInfo = GetBaseEventInfo(inputEvent);
    auto axisAction = OH\_ArkUI\_AxisEvent\_GetAxisAction(inputEvent);
    auto verticalAxis = OH\_ArkUI\_AxisEvent\_GetVerticalAxisValue(inputEvent);
    auto horizontalAxis = OH\_ArkUI\_AxisEvent\_GetHorizontalAxisValue(inputEvent);
    auto pinchAxisScale = OH\_ArkUI\_AxisEvent\_GetPinchAxisScaleValue(inputEvent);
    auto scrollStep = OH\_ArkUI\_AxisEvent\_GetScrollStep(inputEvent);

    OH\_LOG\_Print(LOG\_APP, LOG\_INFO, LOG\_PRINT\_DOMAIN, "\[NdkBindInputEvent\]",
        "\[%{public}s\] Axis: axisAction=%{public}d, verticalAxis=%{public}f, horizontalAxis=%{public}f, "
        "pinchAxisScale=%{public}f, scrollStep=%{public}d, action=%{public}d, "
        "eventTime=%{public}lld, sourceType=%{public}d, type=%{public}d",
        eventName\_.c\_str(), axisAction, verticalAxis, horizontalAxis, pinchAxisScale, scrollStep,
        baseInfo.action, baseInfo.eventTime, baseInfo.sourceType, baseInfo.type);
}

从API version 17开始，新增[NODE\_ON\_AXIS](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-node-h#arkui_nodeeventtype)枚举。节点自身可以注册NODE\_ON\_AXIS事件监听，以响应上述处理函数。

nativeModule\_->registerNodeEvent(handle\_, NODE\_ON\_AXIS, 0, this);

#### 按键事件

按键事件在用户按下或释放键盘按键时触发，可获取按键类型、按键动作、按下的键、修饰键状态、设备ID等信息。组件需要先获取焦点才能响应按键事件。可对节点绑定如下处理函数。

// 处理按键事件
void HandleKeyEvent(ArkUI\_UIInputEvent \*inputEvent)
{
    auto type = OH\_ArkUI\_KeyEvent\_GetType(inputEvent);
    auto baseInfo = GetBaseEventInfo(inputEvent);
    auto deviceId = OH\_ArkUI\_UIInputEvent\_GetDeviceId(inputEvent);
    auto keyInfo = GetKeyInfo(inputEvent);

    OH\_LOG\_Print(LOG\_APP, LOG\_INFO, LOG\_PRINT\_DOMAIN, "\[NdkBindInputEvent\]",
        "\[%{public}s\] Key: type=%{public}d, action=%{public}d, pressedKey=%{public}d, "
        "modifierKeys=%{public}llu, deviceId=%{public}d, eventTime=%{public}lld, sourceType=%{public}d",
        eventName\_.c\_str(), type, baseInfo.action, keyInfo.pressedKey, keyInfo.modifierKeys,
        deviceId, baseInfo.eventTime, baseInfo.sourceType);
}

设置节点为可聚焦，并绑定点击事件，在点击时请求焦点。同时节点自身需要注册[NODE\_ON\_KEY\_EVENT](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-native-node-h#arkui_nodeeventtype)事件监听，以响应上述处理函数。

// 设置为可聚焦，以便接收按键事件
ArkUI\_NumberValue focusable\[\] = {1};
ArkUI\_AttributeItem focusableItem = {focusable, 1};
nativeModule\_->setAttribute(handle\_, NODE\_FOCUSABLE, &focusableItem);

// 注册点击事件，点击时请求焦点
nativeModule\_->registerNodeEvent(handle\_, NODE\_ON\_CLICK\_EVENT, 0, this);

// 注册按键事件
nativeModule\_->registerNodeEvent(handle\_, NODE\_ON\_KEY\_EVENT, 0, this);

#### 完整示例

1.  定义基础输入事件的数据结构。
    
    // InputEventTypes.h
    // 输入事件相关的类型定义和辅助函数
    
    #ifndef MYAPPLICATION\_INPUTEVENTTYPES\_H
    #define MYAPPLICATION\_INPUTEVENTTYPES\_H
    
    #include <cstdint>
    
    namespace NativeModule {
    
    // 基础事件信息结构体
    struct BaseEventInfo {
        int32\_t action;
        int64\_t eventTime;
        int32\_t sourceType;
        int32\_t type;
    };
    
    // 指针坐标信息结构体
    struct PointerPosition {
        float x;
        float y;
        float displayX;
        float displayY;
        float windowX;
        float windowY;
    };
    
    // 按键信息结构体
    struct KeyInfo {
        int32\_t pressedKey;
        uint64\_t modifierKeys;
    };
    
    } // namespace NativeModule
    
    #endif // MYAPPLICATION\_INPUTEVENTTYPES\_H
    
2.  注册各种基础输入事件并实现相应事件的监听。
    
    // NormalTextListExample.h
    // 输入事件示例，展示基础输入事件的绑定和处理
    
    #ifndef MYAPPLICATION\_NORMALTEXTLISTEXAMPLE\_H
    #define MYAPPLICATION\_NORMALTEXTLISTEXAMPLE\_H
    
    #include "ArkUIBaseNode.h"
    #include "ArkUIListItemNode.h"
    #include "ArkUIListNode.h"
    #include "ArkUITextNode.h"
    #include "InputEventTypes.h"
    #include <arkui/native\_key\_event.h>
    #include <hilog/log.h>
    #include <string>
    
    namespace NativeModule {
    
    // 常量定义
    const unsigned int LOG\_PRINT\_DOMAIN = 0xF811;
    const unsigned int ITEM\_HEIGHT = 100;
    const unsigned int FONT\_SIZE = 16;
    
    // 自定义列表项类，用于处理不同类型的输入事件
    class InputEventListItem : public ArkUIListItemNode {
    public:
        enum EventType {
            CLICK\_EVENT,
            TOUCH\_EVENT,
            MOUSE\_EVENT,
            HOVER\_EVENT,
            AXIS\_EVENT,
            KEY\_EVENT
        };
    
        InputEventListItem(const std::string& eventName, uint32\_t bgColor, EventType eventType)
            : eventName\_(eventName), eventType\_(eventType)
        {
            SetPercentWidth(1);
            SetHeight(ITEM\_HEIGHT);
            SetBackgroundColor(bgColor);
    
            auto textNode = std::make\_shared<ArkUITextNode>();
            textNode->SetTextContent(eventName);
            textNode->SetFontSize(FONT\_SIZE);
            textNode->SetFontColor(0xFFFFFFFF);
            textNode->SetTextAlign(ARKUI\_TEXT\_ALIGNMENT\_CENTER);
            textNode->SetPercentWidth(1);
            AddChild(textNode);
    
            // 注册事件监听器
            RegisterEventReceiver();
        }
    
    private:
        // 注册事件接收器和回调
        void RegisterEventReceiver()
        {
            if (!handle\_) {
                return;
            }
            nativeModule\_->addNodeEventReceiver(handle\_, \[\](ArkUI\_NodeEvent \*event) {
                auto \*item = static\_cast<InputEventListItem\*>(OH\_ArkUI\_NodeEvent\_GetUserData(event));
                if (item) {
                    item->HandleEvent(event);
                }
            });
            RegisterEventByType();
        }
    
        // 根据事件类型注册具体事件
        void RegisterEventByType()
        {
            switch (eventType\_) {
                case CLICK\_EVENT:
                    RegisterClickEvent();
                    break;
                case TOUCH\_EVENT:
                    RegisterTouchEvent();
                    break;
                case MOUSE\_EVENT:
                    RegisterMouseEvent();
                    break;
                case HOVER\_EVENT:
                    RegisterHoverEvent();
                    break;
                case AXIS\_EVENT:
                    RegisterAxisEvent();
                    break;
                case KEY\_EVENT:
                    RegisterKeyEvent();
                    break;
            }
        }
    
        // 注册点击事件
        void RegisterClickEvent()
        {
            nativeModule\_->registerNodeEvent(handle\_, NODE\_ON\_CLICK\_EVENT, 0, this);
        }
    
        // 注册触摸事件
        void RegisterTouchEvent()
        {
            nativeModule\_->registerNodeEvent(handle\_, NODE\_TOUCH\_EVENT, 0, this);
        }
    
        // 注册鼠标事件
        void RegisterMouseEvent()
        {
            // 鼠标事件（点击、移动、释放等）
            nativeModule\_->registerNodeEvent(handle\_, NODE\_ON\_MOUSE, 0, this);
        }
    
        // 注册悬浮事件
        void RegisterHoverEvent()
        {
            nativeModule\_->registerNodeEvent(handle\_, NODE\_ON\_HOVER\_EVENT, 0, this);
        }
    
        // 注册轴事件
        void RegisterAxisEvent()
        {
            // 轴事件：鼠标滚轮滚动时触发
            nativeModule\_->registerNodeEvent(handle\_, NODE\_ON\_AXIS, 0, this);
        }
    
        // 注册按键事件
        void RegisterKeyEvent()
        {
            // 设置为可聚焦，以便接收按键事件
            ArkUI\_NumberValue focusable\[\] = {1};
            ArkUI\_AttributeItem focusableItem = {focusable, 1};
            nativeModule\_->setAttribute(handle\_, NODE\_FOCUSABLE, &focusableItem);
    
            // 注册点击事件，点击时请求焦点
            nativeModule\_->registerNodeEvent(handle\_, NODE\_ON\_CLICK\_EVENT, 0, this);
    
            // 注册按键事件
            nativeModule\_->registerNodeEvent(handle\_, NODE\_ON\_KEY\_EVENT, 0, this);
        }
    
        // 事件分发处理
        void HandleEvent(ArkUI\_NodeEvent \*event)
        {
            auto eventType = OH\_ArkUI\_NodeEvent\_GetEventType(event);
            auto \*inputEvent = OH\_ArkUI\_NodeEvent\_GetInputEvent(event);
            auto uiType = OH\_ArkUI\_UIInputEvent\_GetType(inputEvent);
    
            DispatchEvent(eventType, uiType, inputEvent);
        }
    
        // 根据事件类型分发到具体处理函数
        void DispatchEvent(int32\_t eventType, int32\_t uiType, ArkUI\_UIInputEvent \*inputEvent)
        {
            switch (eventType\_) {
                case CLICK\_EVENT:
                    if (eventType == NODE\_ON\_CLICK\_EVENT) {
                        HandleClickEvent(inputEvent);
                    }
                    break;
                case TOUCH\_EVENT:
                    if (eventType == NODE\_TOUCH\_EVENT && uiType == ARKUI\_UIINPUTEVENT\_TYPE\_TOUCH) {
                        HandleTouchEvent(inputEvent);
                    }
                    break;
                case MOUSE\_EVENT:
                    // 鼠标事件（点击、移动、释放等）
                    if (eventType == NODE\_ON\_MOUSE && uiType == ARKUI\_UIINPUTEVENT\_TYPE\_MOUSE) {
                        HandleMouseEvent(inputEvent);
                    }
                    break;
                case HOVER\_EVENT:
                    if (eventType == NODE\_ON\_HOVER\_EVENT) {
                        HandleHoverEvent(inputEvent);
                    }
                    break;
                case AXIS\_EVENT:
                    // 鼠标滚轮滚动或触控板手势时触发
                    if (eventType == NODE\_ON\_AXIS && uiType == ARKUI\_UIINPUTEVENT\_TYPE\_AXIS) {
                        HandleAxisEvent(inputEvent);
                    }
                    break;
                case KEY\_EVENT:
                    if (eventType == NODE\_ON\_CLICK\_EVENT) {
                        // 点击时请求焦点
                        ArkUI\_NumberValue requestFocus\[\] = {1};
                        ArkUI\_AttributeItem requestFocusItem = {requestFocus, 1};
                        nativeModule\_->setAttribute(handle\_, NODE\_FOCUS\_STATUS, &requestFocusItem);
    
                        OH\_LOG\_Print(LOG\_APP, LOG\_INFO, LOG\_PRINT\_DOMAIN, "\[NdkBindInputEvent\]",
                            "\[%{public}s\] Focus requested", eventName\_.c\_str());
                    } else if (eventType == NODE\_ON\_KEY\_EVENT && uiType == ARKUI\_UIINPUTEVENT\_TYPE\_KEY) {
                        // 处理按键事件
                        HandleKeyEvent(inputEvent);
                    }
                    break;
            }
        }
    
        // 获取基础事件信息
        BaseEventInfo GetBaseEventInfo(ArkUI\_UIInputEvent \*inputEvent)
        {
            BaseEventInfo info;
            info.action = OH\_ArkUI\_UIInputEvent\_GetAction(inputEvent);
            info.eventTime = OH\_ArkUI\_UIInputEvent\_GetEventTime(inputEvent);
            info.sourceType = OH\_ArkUI\_UIInputEvent\_GetSourceType(inputEvent);
            info.type = OH\_ArkUI\_UIInputEvent\_GetType(inputEvent);
            return info;
        }
    
        // 获取事件位置信息
        PointerPosition GetPointerPosition(ArkUI\_UIInputEvent \*inputEvent)
        {
            PointerPosition pos;
            pos.x = OH\_ArkUI\_PointerEvent\_GetX(inputEvent);
            pos.y = OH\_ArkUI\_PointerEvent\_GetY(inputEvent);
            pos.displayX = OH\_ArkUI\_PointerEvent\_GetDisplayX(inputEvent);
            pos.displayY = OH\_ArkUI\_PointerEvent\_GetDisplayY(inputEvent);
            pos.windowX = OH\_ArkUI\_PointerEvent\_GetWindowX(inputEvent);
            pos.windowY = OH\_ArkUI\_PointerEvent\_GetWindowY(inputEvent);
            return pos;
        }
    
        // 打印事件位置信息
        void LogPointerPosition(const char \*eventName, const PointerPosition &pos)
        {
            OH\_LOG\_Print(LOG\_APP, LOG\_INFO, LOG\_PRINT\_DOMAIN, "\[NdkBindInputEvent\]",
                "\[%{public}s\] x=%{public}f, y=%{public}f, displayX=%{public}f, displayY=%{public}f, "
                "windowX=%{public}f, windowY=%{public}f",
                eventName, pos.x, pos.y, pos.displayX, pos.displayY, pos.windowX, pos.windowY);
        }
    
        // 处理点击事件
        void HandleClickEvent(ArkUI\_UIInputEvent \*inputEvent)
        {
            auto pos = GetPointerPosition(inputEvent);
            auto baseInfo = GetBaseEventInfo(inputEvent);
            auto pointerCount = OH\_ArkUI\_PointerEvent\_GetPointerCount(inputEvent);
            auto pointerId = OH\_ArkUI\_PointerEvent\_GetPointerId(inputEvent, 0);
            auto pressure = OH\_ArkUI\_PointerEvent\_GetPressure(inputEvent, 0);
    
            // 通过索引获取指针位置信息（支持多点触控场景）
            auto xByIndex = OH\_ArkUI\_PointerEvent\_GetXByIndex(inputEvent, 0);
            auto yByIndex = OH\_ArkUI\_PointerEvent\_GetYByIndex(inputEvent, 0);
            auto displayXByIndex = OH\_ArkUI\_PointerEvent\_GetDisplayXByIndex(inputEvent, 0);
            auto displayYByIndex = OH\_ArkUI\_PointerEvent\_GetDisplayYByIndex(inputEvent, 0);
            auto windowXByIndex = OH\_ArkUI\_PointerEvent\_GetWindowXByIndex(inputEvent, 0);
            auto windowYByIndex = OH\_ArkUI\_PointerEvent\_GetWindowYByIndex(inputEvent, 0);
    
            OH\_LOG\_Print(LOG\_APP, LOG\_INFO, LOG\_PRINT\_DOMAIN, "\[NdkBindInputEvent\]",
                "\[%{public}s\] Click: x=%{public}f, y=%{public}f, displayX=%{public}f, displayY=%{public}f, "
                "windowX=%{public}f, windowY=%{public}f, xByIndex=%{public}f, yByIndex=%{public}f, "
                "displayXByIndex=%{public}f, displayYByIndex=%{public}f, windowXByIndex=%{public}f, "
                "windowYByIndex=%{public}f, pointerCount=%{public}d, pointerId=%{public}d, "
                "pressure=%{public}f, action=%{public}d, eventTime=%{public}lld, sourceType=%{public}d, type=%{public}d",
                eventName\_.c\_str(), pos.x, pos.y, pos.displayX, pos.displayY, pos.windowX, pos.windowY,
                xByIndex, yByIndex, displayXByIndex, displayYByIndex, windowXByIndex, windowYByIndex,
                pointerCount, pointerId, pressure, baseInfo.action, baseInfo.eventTime,
                baseInfo.sourceType, baseInfo.type);
        }
    
        // 处理触摸事件
        void HandleTouchEvent(ArkUI\_UIInputEvent \*inputEvent)
        {
            auto pos = GetPointerPosition(inputEvent);
            auto baseInfo = GetBaseEventInfo(inputEvent);
            auto pointerCount = OH\_ArkUI\_PointerEvent\_GetPointerCount(inputEvent);
            auto pointerId = OH\_ArkUI\_PointerEvent\_GetPointerId(inputEvent, 0);
            auto pressure = OH\_ArkUI\_PointerEvent\_GetPressure(inputEvent, 0);
            auto touchAreaWidth = OH\_ArkUI\_PointerEvent\_GetTouchAreaWidth(inputEvent, 0);
            auto touchAreaHeight = OH\_ArkUI\_PointerEvent\_GetTouchAreaHeight(inputEvent, 0);
    
            // 通过索引获取指针位置信息（支持多点触控场景）
            auto xByIndex = OH\_ArkUI\_PointerEvent\_GetXByIndex(inputEvent, 0);
            auto yByIndex = OH\_ArkUI\_PointerEvent\_GetYByIndex(inputEvent, 0);
            auto displayXByIndex = OH\_ArkUI\_PointerEvent\_GetDisplayXByIndex(inputEvent, 0);
            auto displayYByIndex = OH\_ArkUI\_PointerEvent\_GetDisplayYByIndex(inputEvent, 0);
            auto windowXByIndex = OH\_ArkUI\_PointerEvent\_GetWindowXByIndex(inputEvent, 0);
            auto windowYByIndex = OH\_ArkUI\_PointerEvent\_GetWindowYByIndex(inputEvent, 0);
    
            OH\_LOG\_Print(LOG\_APP, LOG\_INFO, LOG\_PRINT\_DOMAIN, "\[NdkBindInputEvent\]",
                "\[%{public}s\] Touch: x=%{public}f, y=%{public}f, displayX=%{public}f, displayY=%{public}f, "
                "windowX=%{public}f, windowY=%{public}f, xByIndex=%{public}f, yByIndex=%{public}f, "
                "displayXByIndex=%{public}f, displayYByIndex=%{public}f, windowXByIndex=%{public}f, "
                "windowYByIndex=%{public}f, pointerCount=%{public}d, pointerId=%{public}d, "
                "pressure=%{public}f, touchAreaWidth=%{public}f, touchAreaHeight=%{public}f, "
                "action=%{public}d, eventTime=%{public}lld, sourceType=%{public}d, type=%{public}d",
                eventName\_.c\_str(), pos.x, pos.y, pos.displayX, pos.displayY, pos.windowX, pos.windowY,
                xByIndex, yByIndex, displayXByIndex, displayYByIndex, windowXByIndex, windowYByIndex,
                pointerCount, pointerId, pressure, touchAreaWidth, touchAreaHeight, baseInfo.action,
                baseInfo.eventTime, baseInfo.sourceType, baseInfo.type);
        }
    
        // 处理鼠标事件
        void HandleMouseEvent(ArkUI\_UIInputEvent \*inputEvent)
        {
            auto pos = GetPointerPosition(inputEvent);
            auto baseInfo = GetBaseEventInfo(inputEvent);
            auto button = OH\_ArkUI\_MouseEvent\_GetMouseButton(inputEvent);
            auto mouseAction = OH\_ArkUI\_MouseEvent\_GetMouseAction(inputEvent);
            auto rawDeltaX = OH\_ArkUI\_MouseEvent\_GetRawDeltaX(inputEvent);
            auto rawDeltaY = OH\_ArkUI\_MouseEvent\_GetRawDeltaY(inputEvent);
            auto buttonCount = GetPressedButtonsCount(inputEvent);
    
            OH\_LOG\_Print(LOG\_APP, LOG\_INFO, LOG\_PRINT\_DOMAIN, "\[NdkBindInputEvent\]",
                "\[%{public}s\] Mouse: x=%{public}f, y=%{public}f, displayX=%{public}f, displayY=%{public}f, "
                "windowX=%{public}f, windowY=%{public}f, button=%{public}d, mouseAction=%{public}d, "
                "pressedButtonsCount=%{public}d, rawDeltaX=%{public}f, rawDeltaY=%{public}f, "
                "action=%{public}d, eventTime=%{public}lld, sourceType=%{public}d, type=%{public}d",
                eventName\_.c\_str(), pos.x, pos.y, pos.displayX, pos.displayY, pos.windowX, pos.windowY,
                button, mouseAction, buttonCount, rawDeltaX, rawDeltaY, baseInfo.action,
                baseInfo.eventTime, baseInfo.sourceType, baseInfo.type);
        }
    
        // 获取按下的鼠标按键数量
        int32\_t GetPressedButtonsCount(ArkUI\_UIInputEvent \*inputEvent)
        {
            int32\_t pressedButtons\[5\] = {};
            int32\_t length = 5;
            OH\_ArkUI\_MouseEvent\_GetPressedButtons(inputEvent, pressedButtons, &length);
            return length;
        }
    
        // 处理悬浮事件
        void HandleHoverEvent(ArkUI\_UIInputEvent \*inputEvent)
        {
            auto pos = GetPointerPosition(inputEvent);
            auto baseInfo = GetBaseEventInfo(inputEvent);
            auto isHovered = OH\_ArkUI\_HoverEvent\_IsHovered(inputEvent);
    
            OH\_LOG\_Print(LOG\_APP, LOG\_INFO, LOG\_PRINT\_DOMAIN, "\[NdkBindInputEvent\]",
                "\[%{public}s\] Hover: x=%{public}f, y=%{public}f, displayX=%{public}f, displayY=%{public}f, "
                "windowX=%{public}f, windowY=%{public}f, isHovered=%{public}d, action=%{public}d, "
                "eventTime=%{public}lld, sourceType=%{public}d, type=%{public}d",
                eventName\_.c\_str(), pos.x, pos.y, pos.displayX, pos.displayY, pos.windowX, pos.windowY,
                isHovered, baseInfo.action, baseInfo.eventTime, baseInfo.sourceType, baseInfo.type);
        }
    
        // 处理轴事件
        void HandleAxisEvent(ArkUI\_UIInputEvent \*inputEvent)
        {
            auto baseInfo = GetBaseEventInfo(inputEvent);
            auto axisAction = OH\_ArkUI\_AxisEvent\_GetAxisAction(inputEvent);
            auto verticalAxis = OH\_ArkUI\_AxisEvent\_GetVerticalAxisValue(inputEvent);
            auto horizontalAxis = OH\_ArkUI\_AxisEvent\_GetHorizontalAxisValue(inputEvent);
            auto pinchAxisScale = OH\_ArkUI\_AxisEvent\_GetPinchAxisScaleValue(inputEvent);
            auto scrollStep = OH\_ArkUI\_AxisEvent\_GetScrollStep(inputEvent);
    
            OH\_LOG\_Print(LOG\_APP, LOG\_INFO, LOG\_PRINT\_DOMAIN, "\[NdkBindInputEvent\]",
                "\[%{public}s\] Axis: axisAction=%{public}d, verticalAxis=%{public}f, horizontalAxis=%{public}f, "
                "pinchAxisScale=%{public}f, scrollStep=%{public}d, action=%{public}d, "
                "eventTime=%{public}lld, sourceType=%{public}d, type=%{public}d",
                eventName\_.c\_str(), axisAction, verticalAxis, horizontalAxis, pinchAxisScale, scrollStep,
                baseInfo.action, baseInfo.eventTime, baseInfo.sourceType, baseInfo.type);
        }
    
        // 处理按键事件
        void HandleKeyEvent(ArkUI\_UIInputEvent \*inputEvent)
        {
            auto type = OH\_ArkUI\_KeyEvent\_GetType(inputEvent);
            auto baseInfo = GetBaseEventInfo(inputEvent);
            auto deviceId = OH\_ArkUI\_UIInputEvent\_GetDeviceId(inputEvent);
            auto keyInfo = GetKeyInfo(inputEvent);
    
            OH\_LOG\_Print(LOG\_APP, LOG\_INFO, LOG\_PRINT\_DOMAIN, "\[NdkBindInputEvent\]",
                "\[%{public}s\] Key: type=%{public}d, action=%{public}d, pressedKey=%{public}d, "
                "modifierKeys=%{public}llu, deviceId=%{public}d, eventTime=%{public}lld, sourceType=%{public}d",
                eventName\_.c\_str(), type, baseInfo.action, keyInfo.pressedKey, keyInfo.modifierKeys,
                deviceId, baseInfo.eventTime, baseInfo.sourceType);
        }
    
        // 获取按键信息
        KeyInfo GetKeyInfo(ArkUI\_UIInputEvent \*inputEvent)
        {
            KeyInfo info;
            int32\_t pressedKeys\[10\] = {};
            int32\_t length = 10;
            OH\_ArkUI\_UIInputEvent\_GetPressedKeys(inputEvent, pressedKeys, &length);
            info.pressedKey = length > 0 ? pressedKeys\[0\] : -1;
    
            uint64\_t modifierKeys = 0;
            OH\_ArkUI\_UIInputEvent\_GetModifierKeyStates(inputEvent, &modifierKeys);
            info.modifierKeys = modifierKeys;
    
            return info;
        }
    
        std::string eventName\_;
        EventType eventType\_;
    };
    
    // 创建列表项
    std::shared\_ptr<InputEventListItem> CreateEventListItem(
        const std::string& name, uint32\_t color, InputEventListItem::EventType type)
    {
        return std::make\_shared<InputEventListItem>(name, color, type);
    }
    
    // 创建按键事件按钮
    ArkUI\_NodeHandle CreateKeyEventButton(const char\* name, uint32\_t bgColor)
    {
        auto\* nodeAPI = NativeModule::NativeModuleInstance::GetInstance()->GetNativeNodeAPI();
        ArkUI\_NodeHandle buttonHandle = nodeAPI->createNode(ARKUI\_NODE\_BUTTON);
    
        // 设置按钮属性
        ArkUI\_AttributeItem idItem = {.string = "keyEventButton"};
        nodeAPI->setAttribute(buttonHandle, NODE\_ID, &idItem);
    
        ArkUI\_NumberValue typeValue\[\] = {{.i32 = 0}};
        ArkUI\_AttributeItem typeItem = {typeValue, 1};
        nodeAPI->setAttribute(buttonHandle, NODE\_BUTTON\_TYPE, &typeItem);
    
        ArkUI\_NumberValue widthValue\[\] = {{.f32 = 1.0f}};
        ArkUI\_AttributeItem widthItem = {widthValue, 1};
        nodeAPI->setAttribute(buttonHandle, NODE\_WIDTH\_PERCENT, &widthItem);
    
        ArkUI\_NumberValue heightValue\[\] = {{.f32 = 100.0f}};
        ArkUI\_AttributeItem heightItem = {heightValue, 1};
        nodeAPI->setAttribute(buttonHandle, NODE\_HEIGHT, &heightItem);
    
        ArkUI\_NumberValue bgValue\[\] = {{.u32 = bgColor}};
        ArkUI\_AttributeItem bgItem = {bgValue, 1};
        nodeAPI->setAttribute(buttonHandle, NODE\_BACKGROUND\_COLOR, &bgItem);
    
        ArkUI\_AttributeItem labelItem = {.string = name};
        nodeAPI->setAttribute(buttonHandle, NODE\_BUTTON\_LABEL, &labelItem);
    
        ArkUI\_NumberValue focusable\[\] = {{.i32 = 1}};
        ArkUI\_AttributeItem focusableItem = {focusable, 1};
        nodeAPI->setAttribute(buttonHandle, NODE\_FOCUSABLE, &focusableItem);
    
        // 注册事件
        nodeAPI->registerNodeEvent(buttonHandle, NODE\_ON\_CLICK\_EVENT, 0, buttonHandle);
        nodeAPI->registerNodeEvent(buttonHandle, NODE\_ON\_KEY\_EVENT, 0, buttonHandle);
    
        return buttonHandle;
    }
    
    // 处理按键按钮的点击事件
    void HandleKeyEventButtonClick(ArkUI\_NodeEvent \*event)
    {
        ArkUI\_NumberValue requestFocus\[\] = {{.i32 = 1}};
        ArkUI\_AttributeItem requestFocusItem = {requestFocus, 1};
        auto\* nativeModule = NativeModule::NativeModuleInstance::GetInstance()->GetNativeNodeAPI();
        nativeModule->setAttribute(
            OH\_ArkUI\_NodeEvent\_GetNodeHandle(event), NODE\_FOCUS\_STATUS, &requestFocusItem);
    
        OH\_LOG\_Print(LOG\_APP, LOG\_INFO, LOG\_PRINT\_DOMAIN, "\[NdkBindInputEvent\]",
            "\[Key Event Button\] Focus requested via click");
    }
    
    // 处理按键按钮的按键事件
    void HandleKeyEventButtonKey(ArkUI\_NodeEvent \*event)
    {
        auto \*inputEvent = OH\_ArkUI\_NodeEvent\_GetInputEvent(event);
        auto type = OH\_ArkUI\_KeyEvent\_GetType(inputEvent);
        auto action = OH\_ArkUI\_UIInputEvent\_GetAction(inputEvent);
        auto deviceId = OH\_ArkUI\_UIInputEvent\_GetDeviceId(inputEvent);
    
        int32\_t pressedKeys\[10\] = {};
        int32\_t length = 10;
        OH\_ArkUI\_UIInputEvent\_GetPressedKeys(inputEvent, pressedKeys, &length);
        auto pressedKey = length > 0 ? pressedKeys\[0\] : -1;
    
        uint64\_t modifierKeys = 0;
        OH\_ArkUI\_UIInputEvent\_GetModifierKeyStates(inputEvent, &modifierKeys);
    
        OH\_LOG\_Print(LOG\_APP, LOG\_INFO, LOG\_PRINT\_DOMAIN, "\[NdkBindInputEvent\]",
            "\[Key Event Button\] Key: type=%{public}d, action=%{public}d, pressedKey=%{public}d, "
            "modifierKeys=%{public}llu, deviceId=%{public}d",
            type, action, pressedKey, modifierKeys, deviceId);
    }
    
    // 注册按键按钮的事件接收器
    void RegisterKeyEventButtonReceiver(ArkUI\_NodeHandle buttonHandle)
    {
        auto\* nodeAPI = NativeModule::NativeModuleInstance::GetInstance()->GetNativeNodeAPI();
        nodeAPI->addNodeEventReceiver(buttonHandle, \[\](ArkUI\_NodeEvent \*event) {
            auto eventType = OH\_ArkUI\_NodeEvent\_GetEventType(event);
            auto uiType = OH\_ArkUI\_UIInputEvent\_GetType(OH\_ArkUI\_NodeEvent\_GetInputEvent(event));
    
            if (eventType == NODE\_ON\_CLICK\_EVENT) {
                HandleKeyEventButtonClick(event);
            } else if (eventType == NODE\_ON\_KEY\_EVENT && uiType == ARKUI\_UIINPUTEVENT\_TYPE\_KEY) {
                HandleKeyEventButtonKey(event);
            }
        });
    }
    
    // 添加所有事件列表项到列表
    void AddEventListItems(std::shared\_ptr<ArkUIListNode> list)
    {
        auto\* nodeAPI = NativeModule::NativeModuleInstance::GetInstance()->GetNativeNodeAPI();
    
        // 点击事件
        list->AddChild(CreateEventListItem("Click Event - Tap to test", 0xFF1976D2,
            InputEventListItem::CLICK\_EVENT));
        // 触摸事件
        list->AddChild(CreateEventListItem("Touch Event - Touch to test", 0xFF9E9E9E,
            InputEventListItem::TOUCH\_EVENT));
        // 鼠标事件
        list->AddChild(CreateEventListItem("Mouse Event - Mouse over/click to test", 0xFF1976D2,
            InputEventListItem::MOUSE\_EVENT));
        // 悬浮事件
        list->AddChild(CreateEventListItem("Hover Event - Hover to test", 0xFF9E9E9E,
            InputEventListItem::HOVER\_EVENT));
        // 轴事件
        list->AddChild(CreateEventListItem("Axis Event - Use mouse wheel", 0xFF1976D2,
            InputEventListItem::AXIS\_EVENT));
        // 按键事件
        ArkUI\_NodeHandle keyButton = CreateKeyEventButton("Key Event - Click to focus, then press keys", 0xFF9E9E9E);
        RegisterKeyEventButtonReceiver(keyButton);
        nodeAPI->addChild(list->GetHandle(), keyButton);
    }
    
    // 创建输入事件列表的主函数
    std::shared\_ptr<ArkUIBaseNode> CreateTextListExample()
    {
        auto list = std::make\_shared<ArkUIListNode>();
        list->SetPercentWidth(1);
        list->SetPercentHeight(1);
        list->SetScrollBarState(true);
        // 添加所有事件列表项
        AddEventListItems(list);
        return list;
    }
    
    } // namespace NativeModule
    
    #endif // MYAPPLICATION\_NORMALTEXTLISTEXAMPLE\_H
    
    由于使用了日志打印接口[OH\_LOG\_Print](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/capi-log-h#oh_log_print)，需要在CMakeLists.txt中添加对libhilog\_ndk.z.so的引用。
    
    add\_library(entry SHARED napi\_init.cpp NativeEntry.cpp)
    target\_link\_libraries(entry PUBLIC libace\_napi.z.so libace\_ndk.z.so libhilog\_ndk.z.so)