首页 > 其他分享 >OpenHarmony系统解决方案 - 输入法弹出时按返回键原页面返回或应用退出

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

时间:2023-08-22 10:37:05浏览次数:47  
标签:返回 OpenHarmony 输入法 Ace 05 23 08 1718 cpp

问题描述

问题环境

系统版本:OpenHarmony-3.2-Release

问题现象

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

异常效果

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

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

正常效果

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

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

问题原因

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

解决方案

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

let keyboardDelegate = inputMethodEngine.getKeyboardDelegate();
keyboardDelegate.on('keyUp', (keyEvent) {
  if (keyEvent.keyCode === 2) {
    return true;
  }
});


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

修改应用工程内文件,路径: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. 抓取点击返回按键时AceLog日志,发现在点击返回时触发了原应用的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. 追踪返回逻辑,发现是在窗口子系统返回事件回调中触发。

// 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. 抓取窗口子系统日志,发现窗口子系统会把键值分发到输入法(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. 根据上述日志查看分发代码逻辑,发现如果输入法在分发按键事件时,如果返回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. 追踪输入法分发事件代码,发现返回值是触发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();
}

本文作者:TiZizzz

想了解更多关于开源的内容,请访问:

51CTO 开源基础软件社区

https://ost.51cto.com/#bkwz

标签:返回,OpenHarmony,输入法,Ace,05,23,08,1718,cpp
From: https://blog.51cto.com/harmonyos/7186376

相关文章

  • 静态web服务器-根据请求返回指定页面数据
    实现步骤1.获取用户请求资源的路径2.根据请求资源的路径,读取指定文件的数据 3.组装指定文件数据的响应报文,发送给浏览器 4.判断请求的文件在服务端不存在,组装404状态的响应报文,发送给浏览器 示例importsocket#获取用户请求资源的路径#根据请求资源的路径,读取指......
  • 自研静态Web服务器并返回固定⻚⾯数据
    开发自己的静态Web服务器的步骤1.编写一个TCP服务端程序 2.获取浏览器发送的http请求报文数据 3.读取固定页面的数据,把页面数据组装成HTTP响应报文数据发送给浏览器。 4.HTTP响应报文数据发送完成以后,关闭服务与客户端的套接字静态Web服务器-返回固定页面数据的示例代码1......
  • vue_中文输入法情况下, 输入框v-model绑定值中没有输入值但却触发input事件
    今天写的一个搜索框,要求输入字符时不作处理,直到用户点击搜索按钮时才执行搜索逻辑;当用户将搜索框文本删除至空字符串时,执行一次无搜索值的搜索逻辑,用于将表格数据恢复至无筛选;在这个功能上我想当然地使用了input事件用于触发输入事件,但是被测试出bug:当输入法是......
  • HarmonyOS/OpenHarmony应用开发-ArkTS语言渲染控制if/else条件渲染
    ArkTS提供了渲染控制的能力。条件渲染可根据应用的不同状态,使用if、else和elseif渲染对应状态下的UI内容。说明:从APIversion9开始,该接口支持在ArkTS卡片中使用。一、使用规则支持if、else和elseif语句。if、elseif后跟随的条件语句可以使用状态变量。允许在容器组件内使用,通过......
  • Asp.net Core Web API运行后返回的实体属性首字母全为小写(实际应该为大写)
    1、Asp.netCoreWebAPI项目运行后打开swagger,通过点击控制器下的方法,以此点击Tryitout-Execute,然后观察Responsebody中内容,发现所有的实体属性均为小写,如下图,但其实实际的实体属性值首字母为大写,如图二,出现这种情况的解决办法如下:  2、第一步:右键项目中依赖项-管理NuGe......
  • PHP查询MySQL 数据库后返回中文为问号
    面向对象1$conn=newmysqli($servername,$user,$password);3增加$conn->query("setnamesutf8");$pdo=newPDO("mysql:host=$servername",$username,$password);增加$pdo->query("setnamesutf8");  面向过程$conn=mysql......
  • mybatis设置命名格式转换 与 批量插入更新&select查询返回自定义实体类 的sql写法
    在mybatis的配置文件中设置了Java实体类驼峰命名与表属性下划线命名的自动转换。在mybatis中,从接口获取到大量数据之后,将数据集合分批量插入更新到表中。在mybatis中,select查询表数据,返回数据的存储类型为自定义的实体类。1.设置Java实体类驼峰命名与表属性下划线......
  • 社区版idea插件spring assistant开发springboot项目返回jsp
    最近了解到社区版idea没有专门的sringboot,网上网友提供支持说是springboot社区版有几种开发模式:springinitilizer:https://start.spring.io/在线创建springassistant插件支持(具体版本可以去github找)这次我选择第二种,然而在springboot开发返回jsp页面一直报错前端页面报错:后端控......
  • 使用RestTemplate 接收请求返回值中的泛型
    现有一个请求返回值的格式为:{ "success":true, "message":"", "code":200, "result":{ "returnCode":"03AD", "returnMsg":"AD3", "qrcode":"https://xxxxx......
  • webman:返回统一格式的json(v1.5.7)
     一,php代码:1,类代码:app/result/Result.php1234567891011121314151617181920212223242526272829303132333435<?php/*   统一格式的返回json数据*/namespaceapp\result; classResult{    //success:code......