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

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

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

51CTO 开源基础软件社区

https://ost.51cto.com

问题描述

问题环境

系统版本: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;
  }
});

以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
});

定位过程

抓取点击返回按键时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

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

// 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");
    }
    ···
}

抓取窗口子系统日志,发现窗口子系统会把键值分发到输入法(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

根据上述日志查看分发代码逻辑,发现如果输入法在分发按键事件时,如果返回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);
    }
}

追踪输入法分发事件代码,发现返回值是触发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();
}

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

51CTO 开源基础软件社区

https://ost.51cto.com

责任编辑:jianghua 来源: 51CTO 开源基础软件社区
相关推荐

2022-01-09 22:59:32

Windows 11Windows微软

2022-08-03 14:34:59

输入法鸿蒙

2021-08-04 16:36:00

Windows 10Windows微软

2013-07-16 14:47:18

Android EdiEditText不弹出Android开发

2021-10-15 22:17:08

Windows 10Windows微软

2016-03-13 17:35:18

2021-11-11 23:00:50

Windows 10Windows微软

2021-11-06 23:04:10

Windows 10Windows微软

2023-09-09 07:08:37

百度输入法AI

2011-05-05 15:36:25

深信服广域网加速

2009-11-17 10:43:59

ubuntu 9.10输入法解决方法

2010-06-08 18:50:30

OpenSUSE 输入

2012-02-15 11:14:21

搜狗输入法

2010-01-11 10:09:14

Ubuntulinux输入法设置

2010-01-08 16:50:51

Ubuntu SCIM

2021-04-19 11:20:25

Windows 10输入法微软

2023-07-27 14:42:16

开机Logo鸿蒙

2013-06-19 15:12:46

邮件系统混合云应用

2010-05-11 11:42:19

2021-04-20 08:30:23

微信微信输入法张小龙
点赞
收藏

51CTO技术栈公众号