OpenHarmony系統(tǒng)解決方案—輸入法彈出時按返回鍵原頁面返回或應(yīng)用退出
問題描述
問題環(huán)境
系統(tǒng)版本:OpenHarmony-3.2-Release
問題現(xiàn)象
- 打開任意包含輸入組件界面的應(yīng)用,點擊輸入組件彈出輸入法。
- 點擊返回按鍵。
- 輸入法隱藏,原應(yīng)用頁面返回或應(yīng)用退出。
異常效果
點擊返回按鍵,輸入法隱藏,原應(yīng)用頁面返回或應(yīng)用退出。
OpenHarmony系統(tǒng)解決方案 - 輸入法彈出時按返回鍵原頁面返回或應(yīng)用退出-開源基礎(chǔ)軟件社區(qū)
正常效果
點擊返回按鍵,僅隱藏輸入法。
OpenHarmony系統(tǒng)解決方案 - 輸入法彈出時按返回鍵原頁面返回或應(yīng)用退出-開源基礎(chǔ)軟件社區(qū)
問題原因
由于輸入法應(yīng)用是InputMethodExtensionAbility,窗口由自己創(chuàng)建,所以返回按鍵的鍵值指令會被傳遞到原有應(yīng)用上,執(zhí)行原有應(yīng)用的返回邏輯。而輸入法本身可以控制此邏輯,但現(xiàn)在OpenHarmony中的示例輸入法并未控制此邏輯,造成問題。
解決方案
let keyboardDelegate = inputMethodEngine.getKeyboardDelegate();
keyboardDelegate.on('keyUp', (keyEvent) {
if (keyEvent.keyCode === 2) {
return true;
}
});
以KikaInput輸入法應(yīng)用為例,修改keyboardDelegate的keyDown和keyUp的兩個監(jiān)聽回調(diào)。
修改應(yīng)用工程內(nèi)文件,路徑: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日志,發(fā)現(xiàn)在點擊返回時觸發(fā)了原應(yīng)用的ProcessBackPressed函數(shù),導(dǎo)致原頁面返回或應(yīng)用退出。
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
追蹤返回邏輯,發(fā)現(xiàn)是在窗口子系統(tǒng)返回事件回調(diào)中觸發(fā)。
// 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");
}
···
}
抓取窗口子系統(tǒng)日志,發(fā)現(xiàn)窗口子系統(tǒng)會把鍵值分發(fā)到輸入法(dispatch keyEvent to input method)后再分解輸入法的返回分發(fā)到Ace(dispatch keyEvent to ACE)中進(jìn)行處理,當(dāng)按鍵抬起時會觸發(fā)ProcessBackPressed函數(shù)。
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
根據(jù)上述日志查看分發(fā)代碼邏輯,發(fā)現(xiàn)如果輸入法在分發(fā)按鍵事件時,如果返回true則事件不會再向Ace分發(fā)。
// 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);
}
}
追蹤輸入法分發(fā)事件代碼,發(fā)現(xiàn)返回值是觸發(fā)keyboardDelegate.on(type)回調(diào)后返回的值。而keyboardDelegate在輸入法應(yīng)用中被使用,所以改動輸入法應(yīng)用的回調(diào)邏輯即可修復(fù)此現(xiàn)象。
// 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();
}