当前位置:   article > 正文

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

openharmony 输入法

问题描述

问题环境

系统版本:OpenHarmony-3.2-Release

问题现象

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

异常效果

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

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

正常效果

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

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

问题原因

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

解决方案

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

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

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

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

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

定位过程

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

  1. 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
  2. 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
  3. 08-05 23:44:44.248 1287 1287 I C03900/Ace: [flutter_ace_view.cpp(operator())-(3)] Mark 0 id Touch Event Processed
  4. 08-05 23:44:44.250 1718 1718 I C03900/Ace: [event_manager.cpp(DispatchKeyEventNG)-(0)] Use platform to handle this event
  5. 08-05 23:44:44.260 1718 1757 I C03900/Ace: [on_text_changed_listener_impl.cpp(SendKeyboardInfo)-(-1)] [OnTextChangedListenerImpl] KeyboardStatus status: 1
  6. 08-05 23:44:44.260 1718 1757 I C03900/Ace: [on_text_changed_listener_impl.cpp(HandleFunctionKey)-(-1)] [OnTextChangedListenerImpl] Handle function key 0
  7. 08-05 23:44:44.260 1718 1718 E C03900/Ace: [on_text_changed_listener_impl.cpp(operator())-(0)] TextInputAction is not support: 0
  8. 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
  9. 08-05 23:44:44.282 1584 1584 I C03900/Ace: [ui_content_impl.cpp(Background)-(-1)] UIContentImpl: window background
  10. 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
  11. 08-05 23:44:44.282 1584 1584 I C03900/Ace: [jsi_declarative_engine.cpp(UpdateApplicationState)-(0)] JsiDeclarativeEngine UpdateApplicationState, packageName , state: 3
  12. 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
  13. 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
  14. 08-05 23:44:44.298 1584 1584 I C03900/Ace: [pipeline_context.cpp(FlushAnimation)-(0)] scheduleTasks size
  15. 08-05 23:44:44.335 1287 1287 I C03900/Ace: [pan_recognizer.cpp(HandleTouchUpEvent)-(3)] pan recognizer receives 0 touch up event
  16. 08-05 23:44:44.335 1718 1718 I C03900/Ace: [ui_content_impl.cpp(ProcessBackPressed)-(-1)] UIContentImpl: ProcessBackPressed: Platform::AceContainer::OnBackPressed called
  17. 08-05 23:44:44.335 1718 1718 I C03900/Ace: [ui_content_impl.cpp(ProcessBackPressed)-(-1)] UIContentImpl::ProcessBackPressed AceContainer
  18. 08-05 23:44:44.336 1718 1718 W C03900/Ace: [pipeline_context.cpp(GetNavDestinationBackButtonNode)-(0)] navigationNode is null, return on line 725
  19. 08-05 23:44:44.337 1718 1718 E C03900/Ace: [js_view_functions.cpp(ExecuteFunctionWithReturn)-(0)] Error calling onBackPress
  20. 08-05 23:44:44.340 1718 1718 W C03900/Ace: [grid_container_info.cpp(BuildColumnWidth)-(0)] container width not changed.
  21. 08-05 23:44:44.358 1718 1718 W C03900/Ace: [grid_container_info.cpp(BuildColumnWidth)-(0)] container width not changed.
  22. 08-05 23:44:44.360 1287 1287 I C03900/Ace: [flutter_ace_view.cpp(operator())-(3)] Mark 0 id Touch Event Processed
  23. 08-05 23:44:44.363 1718 1718 W C03900/Ace: [rosen_render_context.cpp(ClearFocusState)-(0)] focusStateModifier_ is null, return on line 1083
  24. 08-05 23:44:44.377 1718 1718 I C03900/Ace: [pipeline_context.cpp(OnBackPressed)-(0)] CallRouterBackToPopPage(): frontend accept
  25. 08-05 23:44:44.377 1718 1718 I C03900/Ace: [ui_content_impl.cpp(ProcessBackPressed)-(-1)] UIContentImpl::ProcessBackPressed AceContainer return true
  26. 08-05 23:44:44.381 1718 1718 W C03900/Ace: [grid_container_info.cpp(BuildColumnWidth)-(0)] container width not changed.
  27. 08-05 23:44:44.397 1718 1718 W C03900/Ace: [grid_container_info.cpp(BuildColumnWidth)-(0)] container width not changed.
  28. 08-05 23:44:44.407 1718 1718 W C03900/Ace: [rosen_render_context.cpp(ClearFocusState)-(0)] focusStateModifier_ is null, return on line 1083
  29. 08-05 23:44:44.745 1718 1718 I C03900/Ace: [stage_manager.cpp(operator())-(0)] pageTransition in finish
  30. 08-05 23:44:44.746 1718 1718 I C03900/Ace: [stage_manager.cpp(operator())-(0)] pageTransition exit finish

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

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

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

  1. 08-05 23:59:52.185 1287 1287 D C04200/WindowInputChannel: <78>HandlePointerEvent: Receive pointer event, windowId: 7, action: 2
  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
  3. 08-05 23:59:52.191 1287 1287 D C04200/WindowImpl: <2443>TransferPointerEvent: Transfer pointer event to uiContent
  4. 08-05 23:59:52.219 1718 1718 D C04200/WindowInputChannel: <44>HandleKeyEvent: Receive key event, windowId: 12, keyCode: 2
  5. 08-05 23:59:52.219 1718 1718 I C04200/WindowInputChannel: <104>IsKeyboardEvent: isKeyFN: 0, isKeyboard: 0
  6. 08-05 23:59:52.219 1718 1718 I C04200/WindowInputChannel: <61>HandleKeyEvent: dispatch keyEvent to input method
  7. 08-05 23:59:52.224 1718 1718 I C04200/WindowInputChannel: <66>HandleKeyEvent: dispatch keyEvent to ACE
  8. 08-05 23:59:52.224 1718 1718 D C04200/WindowImpl: <2161>ConsumeKeyEvent: KeyCode: 2, action: 2
  9. 08-05 23:59:52.224 1718 1718 D C04200/WindowImpl: <2174>ConsumeKeyEvent: Transfer key event to uiContent
  10. 08-05 23:59:52.235 1584 1584 D C04200/WindowImpl: <1376>Hide: [Client] Window [name:imeWindow, id:5] Hide, reason:0, withAnimation:0
  11. 08-05 23:59:52.236 1584 1592 D C04200/WindowImpl: <2749>UpdateActiveStatus: window active status: 0, id: 5
  12. 08-05 23:59:52.237 1718 1720 D C04200/WindowImpl: <2749>UpdateActiveStatus: window active status: 1, id: 12
  13. 08-05 23:59:52.237 1718 1720 D C04200/WindowImpl: <2743>UpdateOccupiedAreaChangeInfo: Window Update OccupiedArea, id: 12
  14. 08-05 23:59:52.284 1287 1287 D C04200/WindowInputChannel: <78>HandlePointerEvent: Receive pointer event, windowId: 7, action: 4
  15. 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
  16. 08-05 23:59:52.284 1287 1287 D C04200/WindowImpl: <2443>TransferPointerEvent: Transfer pointer event to uiContent
  17. 08-05 23:59:52.308 1718 1718 D C04200/WindowInputChannel: <44>HandleKeyEvent: Receive key event, windowId: 12, keyCode: 2
  18. 08-05 23:59:52.308 1718 1718 I C04200/WindowInputChannel: <104>IsKeyboardEvent: isKeyFN: 0, isKeyboard: 0
  19. 08-05 23:59:52.308 1718 1718 I C04200/WindowInputChannel: <61>HandleKeyEvent: dispatch keyEvent to input method
  20. 08-05 23:59:52.320 1718 1718 I C04200/WindowInputChannel: <66>HandleKeyEvent: dispatch keyEvent to ACE
  21. 08-05 23:59:52.321 1718 1718 D C04200/WindowImpl: <2161>ConsumeKeyEvent: KeyCode: 2, action: 3
  22. 08-05 23:59:52.321 1718 1718 D C04200/WindowImpl: <2129>HandleBackKeyPressedEvent: Transfer back key event to uiContent
  23. 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
  24. 08-05 23:59:52.731 1584 1584 D C04200/WindowImpl: <1376>Hide: [Client] Window [name:imeWindow, id:5] Hide, reason:0, withAnimation:0
  25. 08-05 23:59:52.731 1584 1584 D C04200/WindowImpl: <1389>Hide: window is already hidden id: 5
  26. 08-05 23:59:52.736 1584 1584 D C04200/WindowImpl: <1376>Hide: [Client] Window [name:imeWindow, id:5] Hide, reason:0, withAnimation:0
  27. 08-05 23:59:52.736 1584 1584 D C04200/WindowImpl: <1389>Hide: window is already hidden id: 5

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

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

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

  1. // base/inputmethod/imf/interfaces/kits/js/napi/inputmethodability/js_keyboard_delegate_setting.cpp
  2. bool JsKeyboardDelegateSetting::OnKeyEvent(int32_t keyCode, int32_t keyStatus){
  3. IMSA_HILOGD("run in");
  4. KeyEventPara para{ keyCode, keyStatus, false };
  5. std::string type = (keyStatus == ARGC_TWO ? "keyDown" : "keyUp");
  6. auto isDone = std::make_shared<BlockData<bool>>(MAX_TIMEOUT, false);
  7. uv_work_t *work = GetUVwork(type, [¶, isDone](UvEntry &entry) {
  8. entry.keyEventPara = { para.keyCode, para.keyStatus, para.isOnKeyEvent };
  9. entry.isDone = isDone;
  10. });
  11. if (work == nullptr) {
  12. IMSA_HILOGE("failed to get uv work");
  13. return false;
  14. }
  15. uv_queue_work(
  16. loop_, work, [](uv_work_t *work) {},
  17. [](uv_work_t *work, int status) {
  18. std::shared_ptr<UvEntry> entry(static_cast<UvEntry *>(work->data), [work](UvEntry *data) {
  19. delete data;
  20. delete work;
  21. });
  22. auto getKeyEventProperty = [entry](napi_value *args, uint8_t argc,
  23. std::shared_ptr<JSCallbackObject> item) -> bool {
  24. if (argc == 0) {
  25. return false;
  26. }
  27. napi_value jsObject =
  28. GetResultOnKeyEvent(item->env_, entry->keyEventPara.keyCode, entry->keyEventPara.keyStatus);
  29. if (jsObject == nullptr) {
  30. IMSA_HILOGE("get GetResultOnKeyEvent failed: jsObject is nullptr");
  31. return false;
  32. }
  33. args[ARGC_ZERO] = { jsObject };
  34. return true;
  35. };
  36. bool isOnKeyEvent = JsUtils::TraverseCallback(entry->vecCopy, ARGC_ONE, getKeyEventProperty);
  37. entry->isDone->SetValue(isOnKeyEvent);
  38. });
  39. return isDone->GetValue();
  40. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/304521
推荐阅读
相关标签
  

闽ICP备14008679号