From fa4d15c751277bd197548e111fe23f944e88c878 Mon Sep 17 00:00:00 2001 From: cjand <1747143535@qq.com> Date: Tue, 22 Oct 2024 14:34:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=A4=96=E6=8E=A5=E9=94=AE?= =?UTF-8?q?=E7=9B=98=E8=8E=B7=E5=8F=96=E7=84=A6=E7=82=B9=E5=90=8E=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E8=BE=93=E5=85=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: cjand <1747143535@qq.com> --- .../systemchannels/TextInputChannel.ets | 2 ++ .../ets/embedding/ohos/FlutterAbility.ets | 1 + .../main/ets/embedding/ohos/FlutterEntry.ets | 1 + .../ets/plugin/editing/TextInputPlugin.ets | 29 +++++++++++++++++++ 4 files changed, 33 insertions(+) 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 df71b3fb15..eb6eb64f59 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 4013fed189..edfb59ca4c 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 124eb6488d..38f0192ea0 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 2d5c4ac2a3..8c75488d7a 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; -- Gitee