diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/systemchannels/TextInputChannel.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/systemchannels/TextInputChannel.ets index df71b3fb156cc5220a0f76d2213783d509ba8c82..eb6eb64f598afa3e74700515599d701b44edd656 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/systemchannels/TextInputChannel.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/systemchannels/TextInputChannel.ets @@ -182,6 +182,8 @@ export interface TextInputMethodHandler { clearClient(): void; + handleChangeFocus(focusState: boolean): void; + } export class Configuration { diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets index 4013fed1897c82c3aa82c1b8480ff80873f29af0..edfb59ca4cfbee3525a1857aa6f506cb656190e8 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbility.ets @@ -151,6 +151,7 @@ export class FlutterAbility extends UIAbility implements Host { case window.WindowStageEventType.ACTIVE: // 获焦状态 Log.i(TAG, 'windowStage active.'); if (this.stillAttachedForEvent("onWindowFocusChanged")) { + this.delegate?.getFlutterEngine()?.getTextInputChannel()?.textInputMethodHandler?.handleChangeFocus(true); this?.delegate?.onWindowFocusChanged(true); } break; diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterEntry.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterEntry.ets index 124eb6488d1a9d8062a3d9133f34564e798cd57d..38f0192ea066838fc8578c64bd7212ccd363e3ad 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterEntry.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterEntry.ets @@ -75,6 +75,7 @@ export default class FlutterEntry implements Host { break; case window.WindowStageEventType.ACTIVE: // 获焦状态 Log.i(TAG, 'windowStage active.'); + this.delegate?.getFlutterEngine()?.getTextInputChannel()?.textInputMethodHandler?.handleChangeFocus(true); this?.delegate?.onWindowFocusChanged(true); break; case window.WindowStageEventType.INACTIVE: // 失焦状态 diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextInputPlugin.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextInputPlugin.ets index 2d5c4ac2a3126a34a95acdfd54605be16dc2b005..8c75488d7abf4d293511f4cf695106858d3873b4 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextInputPlugin.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextInputPlugin.ets @@ -22,6 +22,7 @@ import inputMethod from '@ohos.inputMethod'; import Log from '../../util/Log'; import { EditingStateWatcher, ListenableEditingState } from './ListenableEditingState'; import Any from '../common/Any'; +import { inputDevice } from '@kit.InputKit'; /// 临时规避缺少newline对应枚举问题 const NEWLINE_KEY_TYPE: number = 8; @@ -82,6 +83,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { private inputTypeNone: string = 'NONE'; private keyboardStatus: inputMethod.KeyboardStatus = inputMethod.KeyboardStatus.HIDE; private inputAttribute: inputMethod.InputAttribute = { textInputType: 0, enterKeyType: 1 }; + private keyboardFocusState: boolean = false constructor(plugin: TextInputPlugin | Any) { this.textConfig = { @@ -96,6 +98,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { /// 通过判断是否是TextInputType.none来决定是否弹出键盘 show(): void { if (this.canShowTextInput()) { + this.keyboardFocusState = true; this.showTextInput(); } else { this.hide(); @@ -103,6 +106,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { } hide(): void { + this.keyboardFocusState = false; this.hideTextInput(); } @@ -163,6 +167,31 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { } } + handleChangeFocus(focusState: boolean) { + try { + inputDevice.getDeviceList((Error: Error, ids: Array) => { + let isPhysicalKeyboard = false; + for (let i = 0; i < ids.length; i++) { + const type = inputDevice.getKeyboardTypeSync(ids[i]); + if (type == inputDevice.KeyboardType.ALPHABETIC_KEYBOARD || type == inputDevice.KeyboardType.DIGITAL_KEYBOARD) { + isPhysicalKeyboard = true; + break; + } + } + + if(focusState && isPhysicalKeyboard && this.keyboardFocusState) { + this.cancelListenKeyBoardEvent(); + this.inputMethodController.detach().then(async () =>{ + await this.attach(true); + this.listenKeyBoardEvent(); + }) + } + }) + } catch (error) { + Log.e(TextInputMethodHandlerImpl.TAG, `Failed to query device. Code is ${error.code}, message is ${error.message}`) + } + } + async updateAttribute(): Promise { if (this.keyboardStatus != inputMethod.KeyboardStatus.SHOW) { return;