登录
首页 > 程序代码 > OpenHarmony系统解决方案—输入法弹出时按返回键原页面返回或应用退出

OpenHarmony系统解决方案—输入法弹出时按返回键原页面返回或应用退出

发布时间:2023-08-13 18:44:59 发布用户: admin

问题描述

问题环境

系统版本:OpenHarmony-3.2-Release

问题现象

  • 打开任意包含输入组件界面的应用,点击输入组件弹出输入法。
  • 点击返回按键。
  • 输入法隐藏,原应用页面返回或应用退出。

异常效果

点击返回按键,输入法隐藏,原应用页面返回或应用退出。

OpenHarmony系统解决方案 - 输入法弹出时按返回键原页面返回或应用退出-开源基础软件社区OpenHarmony系统解决方案 - 输入法弹出时按返回键原页面返回或应用退出-开源基础软件社区

正常效果

点击返回按键,仅隐藏输入法。

OpenHarmony系统解决方案 - 输入法弹出时按返回键原页面返回或应用退出-开源基础软件社区OpenHarmony系统解决方案 - 输入法弹出时按返回键原页面返回或应用退出-开源基础软件社区

问题原因

由于输入法应用是InputMethodExtensionAbility,窗口由自己创建,所以返回按键的键值指令会被传递到原有应用上,执行原有应用的返回逻辑。而输入法本身可以控制此逻辑,但现在OpenHarmony中的示例输入法并未控制此逻辑,造成问题。

解决方案

输入法应用监听键盘事件时需对返回按键做特殊处理,返回键的keyCode2

let keyboardDelegate = inputMethodEngine.getKeyboardDelegate();
keyboardDelegate.on('keyUp', (keyEvent) {
  if (keyEvent.keyCode === 2) {
    return true;
  }
});
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
 

以KikaInput输入法应用为例,修改keyboardDelegate的keyDown和keyUp的两个监听回调。

修改应用工程内文件,路径:entry\src\main\ets\model\KeyboardController.ets

this.mKeyboardDelegate.on('keyDown', (keyEvent) => {
  if (this.isKeyboardShow && keyEvent.keyCode !== 2) {
    this.inputHandle.hideKeyboardSelf();
  }
  this.inputHandle.addLog('keyDown: code = ' + keyEvent.keyCode);
  let result = this.onKeyDown(keyEvent);
  this.inputHandle.addLog('keyDown: result = ' + result);
  return result
});

this.mKeyboardDelegate.on('keyUp', (keyEvent) => {
  this.inputHandle.addLog('keyUp: code = ' + keyEvent.keyCode);
  if (this.isKeyboardShow && keyEvent.keyCode === 2) {
    this.inputHandle.hideKeyboardSelf();
    return true;
  }
  let result = this.onKeyUp(keyEvent);
  this.inputHandle.addLog('keyUp: result = ' + result);
  return result
});
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
 

定位过程

抓取点击返回按键时Ace的Log日志,发现在点击返回时触发了原应用的ProcessBackPressed函数,导致原页面返回或应用退出。

08-05 23:44:44.248  1718  1718 I C03900/Ace: [ui_content_impl.cpp(ProcessKeyEvent)-(-1)] UIContentImpl: OnKeyUp called,touchEvent info: keyCode is 2,keyAction is 2, keyActionTime is 60014731
08-05 23:44:44.248  1287  1287 I C03900/Ace: [pan_recognizer.cpp(HandleTouchDownEvent)-(3)] pan recognizer receives 0 touch down event, begin to detect pan event
08-05 23:44:44.248  1287  1287 I C03900/Ace: [flutter_ace_view.cpp(operator())-(3)] Mark 0 id Touch Event Processed
08-05 23:44:44.250  1718  1718 I C03900/Ace: [event_manager.cpp(DispatchKeyEventNG)-(0)] Use platform to handle this event
08-05 23:44:44.260  1718  1757 I C03900/Ace: [on_text_changed_listener_impl.cpp(SendKeyboardInfo)-(-1)] [OnTextChangedListenerImpl] KeyboardStatus status: 1
08-05 23:44:44.260  1718  1757 I C03900/Ace: [on_text_changed_listener_impl.cpp(HandleFunctionKey)-(-1)] [OnTextChangedListenerImpl] Handle function key 0
08-05 23:44:44.260  1718  1718 E C03900/Ace: [on_text_changed_listener_impl.cpp(operator())-(0)] TextInputAction  is not support: 0
08-05 23:44:44.277  1718  1720 I C03900/Ace: [ui_content_impl.cpp(OnSizeChange)-(-1)] UIContent::OccupiedAreaChange rect:Rect (0.00, 0.00) - [0.00 x 0.00] type: 0
08-05 23:44:44.282  1584  1584 I C03900/Ace: [ui_content_impl.cpp(Background)-(-1)] UIContentImpl: window background
08-05 23:44:44.282  1718  1718 I C03900/Ace: [pipeline_context.cpp(OnVirtualKeyboardHeightChange)-(0)] OnVirtualKeyboardAreaChange positionY:46.000000 safeArea:1136.000000 offsetFix:-545.000000, keyboardHeight 0.000000
08-05 23:44:44.282  1584  1584 I C03900/Ace: [jsi_declarative_engine.cpp(UpdateApplicationState)-(0)] JsiDeclarativeEngine UpdateApplicationState, packageName , state: 3
08-05 23:44:44.282  1718  1718 I C03900/Ace: [pipeline_context.cpp(SetRootRect)-(0)] SetRootRect width 720.000000, height 1136.000000, 0.000000
08-05 23:44:44.289  1584  1584 I C03900/Ace: [pipeline_context.cpp(OnVirtualKeyboardHeightChange)-(0)] OnVirtualKeyboardAreaChange positionY:0.000000 safeArea:550.000000 offsetFix:-275.000000
08-05 23:44:44.298  1584  1584 I C03900/Ace: [pipeline_context.cpp(FlushAnimation)-(0)] scheduleTasks size
08-05 23:44:44.335  1287  1287 I C03900/Ace: [pan_recognizer.cpp(HandleTouchUpEvent)-(3)] pan recognizer receives 0 touch up event
08-05 23:44:44.335  1718  1718 I C03900/Ace: [ui_content_impl.cpp(ProcessBackPressed)-(-1)] UIContentImpl: ProcessBackPressed: Platform::AceContainer::OnBackPressed called
08-05 23:44:44.335  1718  1718 I C03900/Ace: [ui_content_impl.cpp(ProcessBackPressed)-(-1)] UIContentImpl::ProcessBackPressed AceContainer
08-05 23:44:44.336  1718  1718 W C03900/Ace: [pipeline_context.cpp(GetNavDestinationBackButtonNode)-(0)] navigationNode is null, return on line 725
08-05 23:44:44.337  1718  1718 E C03900/Ace: [js_view_functions.cpp(ExecuteFunctionWithReturn)-(0)] Error calling onBackPress
08-05 23:44:44.340  1718  1718 W C03900/Ace: [grid_container_info.cpp(BuildColumnWidth)-(0)] container width not changed.
08-05 23:44:44.358  1718  1718 W C03900/Ace: [grid_container_info.cpp(BuildColumnWidth)-(0)] container width not changed.
08-05 23:44:44.360  1287  1287 I C03900/Ace: [flutter_ace_view.cpp(operator())-(3)] Mark 0 id Touch Event Processed
08-05 23:44:44.363  1718  1718 W C03900/Ace: [rosen_render_context.cpp(ClearFocusState)-(0)] focusStateModifier_ is null, return on line 1083
08-05 23:44:44.377  1718  1718 I C03900/Ace: [pipeline_context.cpp(OnBackPressed)-(0)] CallRouterBackToPopPage(): frontend accept
08-05 23:44:44.377  1718  1718 I C03900/Ace: [ui_content_impl.cpp(ProcessBackPressed)-(-1)] UIContentImpl::ProcessBackPressed AceContainer return true
08-05 23:44:44.381  1718  1718 W C03900/Ace: [grid_container_info.cpp(BuildColumnWidth)-(0)] container width not changed.
08-05 23:44:44.397  1718  1718 W C03900/Ace: [grid_container_info.cpp(BuildColumnWidth)-(0)] container width not changed.
08-05 23:44:44.407  1718  1718 W C03900/Ace: [rosen_render_context.cpp(ClearFocusState)-(0)] focusStateModifier_ is null, return on line 1083
08-05 23:44:44.745  1718  1718 I C03900/Ace: [stage_manager.cpp(operator())-(0)] pageTransition in finish
08-05 23:44:44.746  1718  1718 I C03900/Ace: [stage_manager.cpp(operator())-(0)] pageTransition exit finish
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
 

追踪返回逻辑,发现是在窗口子系统返回事件回调中触发。

// foundation/window/window_manager/wm/src/window_impl.cpp
void WindowImpl::HandleBackKeyPressedEvent(const std::shared_ptr<MMI::KeyEvent>& keyEvent){
    std::shared_ptr<IInputEventConsumer> inputEventConsumer;
    {
        std::lock_guard<std::recursive_mutex> lock(mutex_);
        inputEventConsumer = inputEventConsumer_;
    }
    bool isConsumed = false;
    if (inputEventConsumer != nullptr) {
        WLOGFD("Transfer back key event to inputEventConsumer");
        isConsumed = inputEventConsumer->OnInputEvent(keyEvent);
    } else if (uiContent_ != nullptr) {
        WLOGFD("Transfer back key event to uiContent");
        isConsumed = uiContent_->ProcessBackPressed();
    } else {
        WLOGFE("There is no back key event consumer");
    }
    ···
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
 

抓取窗口子系统日志,发现窗口子系统会把键值分发到输入法(dispatch keyEvent to input method)后再分解输入法的返回分发到Ace(dispatch keyEvent to ACE)中进行处理,当按键抬起时会触发ProcessBackPressed函数。

08-05 23:59:52.185  1287  1287 D C04200/WindowInputChannel: <78>HandlePointerEvent: Receive pointer event, windowId: 7, action: 2
08-05 23:59:52.186  1287  1287 D C04200/WindowImpl: <2526>ConsumePointerEvent: WMS process point down, window: [name:SystemUi_NavigationBar, id:7], action: 2
08-05 23:59:52.191  1287  1287 D C04200/WindowImpl: <2443>TransferPointerEvent: Transfer pointer event to uiContent
08-05 23:59:52.219  1718  1718 D C04200/WindowInputChannel: <44>HandleKeyEvent: Receive key event, windowId: 12, keyCode: 2
08-05 23:59:52.219  1718  1718 I C04200/WindowInputChannel: <104>IsKeyboardEvent: isKeyFN: 0, isKeyboard: 0
08-05 23:59:52.219  1718  1718 I C04200/WindowInputChannel: <61>HandleKeyEvent: dispatch keyEvent to input method
08-05 23:59:52.224  1718  1718 I C04200/WindowInputChannel: <66>HandleKeyEvent: dispatch keyEvent to ACE
08-05 23:59:52.224  1718  1718 D C04200/WindowImpl: <2161>ConsumeKeyEvent: KeyCode: 2, action: 2
08-05 23:59:52.224  1718  1718 D C04200/WindowImpl: <2174>ConsumeKeyEvent: Transfer key event to uiContent
08-05 23:59:52.235  1584  1584 D C04200/WindowImpl: <1376>Hide: [Client] Window [name:imeWindow, id:5] Hide, reason:0, withAnimation:0
08-05 23:59:52.236  1584  1592 D C04200/WindowImpl: <2749>UpdateActiveStatus: window active status: 0, id: 5
08-05 23:59:52.237  1718  1720 D C04200/WindowImpl: <2749>UpdateActiveStatus: window active status: 1, id: 12
08-05 23:59:52.237  1718  1720 D C04200/WindowImpl: <2743>UpdateOccupiedAreaChangeInfo: Window Update OccupiedArea, id: 12
08-05 23:59:52.284  1287  1287 D C04200/WindowInputChannel: <78>HandlePointerEvent: Receive pointer event, windowId: 7, action: 4
08-05 23:59:52.284  1287  1287 D C04200/WindowImpl: <2416>ConsumeMoveOrDragEvent: [Client Point Up/Cancel]: windowId: 7, action: 4, sourceType: 2, startMove: 0, startDrag: 0
08-05 23:59:52.284  1287  1287 D C04200/WindowImpl: <2443>TransferPointerEvent: Transfer pointer event to uiContent
08-05 23:59:52.308  1718  1718 D C04200/WindowInputChannel: <44>HandleKeyEvent: Receive key event, windowId: 12, keyCode: 2
08-05 23:59:52.308  1718  1718 I C04200/WindowInputChannel: <104>IsKeyboardEvent: isKeyFN: 0, isKeyboard: 0
08-05 23:59:52.308  1718  1718 I C04200/WindowInputChannel: <61>HandleKeyEvent: dispatch keyEvent to input method
08-05 23:59:52.320  1718  1718 I C04200/WindowInputChannel: <66>HandleKeyEvent: dispatch keyEvent to ACE
08-05 23:59:52.321  1718  1718 D C04200/WindowImpl: <2161>ConsumeKeyEvent: KeyCode: 2, action: 3
08-05 23:59:52.321  1718  1718 D C04200/WindowImpl: <2129>HandleBackKeyPressedEvent: Transfer back key event to uiContent
08-05 23:59:52.362  1718  1718 D C04200/WindowImpl: <2135>HandleBackKeyPressedEvent: Back key event is consumed or it is not a main window
08-05 23:59:52.731  1584  1584 D C04200/WindowImpl: <1376>Hide: [Client] Window [name:imeWindow, id:5] Hide, reason:0, withAnimation:0
08-05 23:59:52.731  1584  1584 D C04200/WindowImpl: <1389>Hide: window is already hidden id: 5
08-05 23:59:52.736  1584  1584 D C04200/WindowImpl: <1376>Hide: [Client] Window [name:imeWindow, id:5] Hide, reason:0, withAnimation:0
08-05 23:59:52.736  1584  1584 D C04200/WindowImpl: <1389>Hide: window is already hidden id: 5
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
 

根据上述日志查看分发代码逻辑,发现如果输入法在分发按键事件时,如果返回true则事件不会再向Ace分发。

// foundation/window/window_manager/wm/src/window_input_channel.cpp
void WindowInputChannel::HandleKeyEvent(std::shared_ptr<MMI::KeyEvent>& keyEvent)
{
    ···
    bool inputMethodHasProcessed = false;
#ifdef IMF_ENABLE
    bool isKeyboardEvent = IsKeyboardEvent(keyEvent);
    if (isKeyboardEvent) {
        WLOGFI("dispatch keyEvent to input method");
        inputMethodHasProcessed = MiscServices::InputMethodController::GetInstance()->dispatchKeyEvent(keyEvent);
    }
#endif // IMF_ENABLE
    if (!inputMethodHasProcessed) {
        WLOGFI("dispatch keyEvent to ACE");
        window_->ConsumeKeyEvent(keyEvent);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
 

追踪输入法分发事件代码,发现返回值是触发keyboardDelegate.on(type)回调后返回的值。而keyboardDelegate在输入法应用中被使用,所以改动输入法应用的回调逻辑即可修复此现象。

// base/inputmethod/imf/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp
bool JsKeyboardDelegateSetting::OnKeyEvent(int32_t keyCode, int32_t keyStatus){
    IMSA_HILOGD("run in");
    KeyEventPara para{ keyCode, keyStatus, false };
    std::string type = (keyStatus == ARGC_TWO ? "keyDown" : "keyUp");
    auto isDone = std::make_shared<BlockData<bool>>(MAX_TIMEOUT, false);
    uv_work_t *work = GetUVwork(type, [¶, isDone](UvEntry &entry) {
        entry.keyEventPara = { para.keyCode, para.keyStatus, para.isOnKeyEvent };
        entry.isDone = isDone;
    });
    if (work == nullptr) {
        IMSA_HILOGE("failed to get uv work");
        return false;
    }
    uv_queue_work(
        loop_, work, [](uv_work_t *work) {},
        [](uv_work_t *work, int status) {
            std::shared_ptr<UvEntry> entry(static_cast<UvEntry *>(work->data), [work](UvEntry *data) {
                delete data;
                delete work;
            });
            auto getKeyEventProperty = [entry](napi_value *args, uint8_t argc,
                                           std::shared_ptr<JSCallbackObject> item) -> bool {
                if (argc == 0) {
                    return false;
                }
                napi_value jsObject =
                    GetResultOnKeyEvent(item->env_, entry->keyEventPara.keyCode, entry->keyEventPara.keyStatus);
                if (jsObject == nullptr) {
                    IMSA_HILOGE("get GetResultOnKeyEvent failed: jsObject is nullptr");
                    return false;
                }
                args[ARGC_ZERO] = { jsObject };
                return true;
            };
            bool isOnKeyEvent = JsUtils::TraverseCallback(entry->vecCopy, ARGC_ONE, getKeyEventProperty);
            entry->isDone->SetValue(isOnKeyEvent);
        });
    return isDone->GetValue();
}

PHP更多>>

OpenAI虚晃一枪,但等来了谷歌的AI搜索产品 话剧《苏东坡》南宁首演,让观众与苏东坡穿越千年“对话” 九冠王+AI定义再进化 小鹏X9懂MPV更懂用户 别幻想“价格战”停下来,现在4S店的库存车,比你想的要多 中保研公布10款车型测评成绩:合资表现稳定,自主参差不齐 10万级纯电小型车“大热门”,比亚迪海豚荣耀版该怎么选? 被曝或将采用DM-i超级混动,丰田在国内“投靠”比亚迪,能行吗? 车高增21mm,涉水深度增20mm,这款方盒子又升级了 别克GL8推插电混动版,续航可超1000km,势要将失去的都夺回来? 极狐汽车秀出智慧出行“黑科技”:达尔文2.0技术 2024北京车展前瞻:有看头的轿车特别多! 2024年3月TOP30 SUV销量投诉量对应点评 多车企布局端到端,成本低效果好,华为还能领先多久? 春日赏花必备神器,皓影eHEV打开春游新思路! 日产在华销量劲增,如何摆脱国产新能源的围堵? 悦也双车型齐发,天舆架构亮相——宝骏的这场发布会干货有点多 试驾第9代凯美瑞!换车新选择,20万多项配置同级领先 推动以旧换新政策 助力山东汽车消费 2024齐鲁国际车展今日开幕 如何给孩子一个精彩假期?东风标致新5008给你更多出行灵感! 以旧换新最高补贴5000,2024齐鲁国际车展开幕 特斯拉亮相齐鲁国际车展 Model 3/Y 推出限时低息置换政策 别只看价格,长安Lumin告诉你买三门四座小车最该关注什么 预算15万左右想买合资轿车,别克君威、大众速腾,谁更值得选? 快评新款智界S7:价格不变,入门续航超700公里,更值了? 如何给孩子一个精彩假期?东风标致新5008给你更多出行灵感! 合资车企该换换思路了 行业领先的技术进化 极狐汽车达尔文2.0技术品牌正式发布 寻找新的增长点,哪吒L增程版曝光,满配的打法能出奇效吗? 打卡新捷达 做最酷年轻人 捷达智能机器人全新捷达VS5/VS7潮酷而来 第9代凯美瑞全面到店上市 19.98万价值满配!
Copyright 2018-2023 黑鸟云 版权所有  京ICP备2018062516号-1