From 5cf5a5a7aae48787fabeb8917435b459904273ce Mon Sep 17 00:00:00 2001 From: hezhengyi Date: Sat, 2 Nov 2024 14:39:12 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=94=AF=E6=8C=81ohos=E6=A8=A1=E6=8B=9F?= =?UTF-8?q?=E5=99=A8=E4=BD=BF=E7=94=A8=E7=89=A9=E7=90=86=E9=94=AE=E7=9B=98?= =?UTF-8?q?=E8=BE=93=E5=85=A5=E5=AD=97=E6=AF=8D=E6=95=B0=E5=AD=97=E5=92=8C?= =?UTF-8?q?=E7=89=B9=E6=AE=8A=E7=AC=A6=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hezhengyi --- .../systemchannels/TextInputChannel.ets | 5 +- .../ohos/FlutterAbilityAndEntryDelegate.ets | 4 - .../ohos/KeyEventHandlerForEmulator.ets | 171 ++++++++++++++++++ .../ets/embedding/ohos/KeyboardManager.ets | 7 +- .../ets/plugin/editing/TextInputPlugin.ets | 8 + .../flutter/src/main/ets/view/FlutterView.ets | 6 +- 6 files changed, 193 insertions(+), 8 deletions(-) create mode 100644 shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyEventHandlerForEmulator.ets 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 99a9d4f39c..2515a63c43 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 @@ -40,9 +40,10 @@ export default class TextInputChannel { this.channel = new MethodChannel(dartExecutor, TextInputChannel.CHANNEL_NAME, JSONMethodCodec.INSTANCE); } - setTextInputMethodHandler(textInputMethodHandler: TextInputMethodHandler): void { + setTextInputMethodHandler(textInputMethodHandler: TextInputMethodHandler | null): void { this.textInputMethodHandler = textInputMethodHandler; - this.channel.setMethodCallHandler(new TextInputCallback(this.textInputMethodHandler)); + this.channel.setMethodCallHandler(textInputMethodHandler == null + ? null : new TextInputCallback(textInputMethodHandler)); } requestExistingInputState(): void { diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbilityAndEntryDelegate.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbilityAndEntryDelegate.ets index 480371d807..48e85d9b34 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbilityAndEntryDelegate.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterAbilityAndEntryDelegate.ets @@ -27,7 +27,6 @@ import FlutterInjector from '../../FlutterInjector'; import UIAbility from '@ohos.app.ability.UIAbility'; import ExclusiveAppComponent from './ExclusiveAppComponent'; import AbilityConstant from '@ohos.app.ability.AbilityConstant'; -import TextInputPlugin from '../../plugin/editing/TextInputPlugin'; import { FlutterPlugin } from '../engine/plugins/FlutterPlugin'; import FlutterEngineCache from '../engine/FlutterEngineCache'; import FlutterEngineGroupCache from '../engine/FlutterEngineGroupCache'; @@ -51,7 +50,6 @@ class FlutterAbilityAndEntryDelegate implements ExclusiveAppComponent flutterEngine?: FlutterEngine | null; platformPlugin?: PlatformPlugin; protected context?: common.Context; - protected textInputPlugin?: TextInputPlugin; protected isFlutterEngineFromHostOrCache: boolean = false; private engineGroup?: FlutterEngineGroup; private isHost:boolean = false; @@ -87,7 +85,6 @@ class FlutterAbilityAndEntryDelegate implements ExclusiveAppComponent //configureFlutterEngine this.isAttached = true; if (this.flutterEngine) { - this.textInputPlugin = new TextInputPlugin(this.flutterEngine.getTextInputChannel()!); this.flutterEngine.getSystemLanguages(); } if (this.flutterEngine && this.flutterView && this.host?.attachToEngineAutomatically()) { @@ -271,7 +268,6 @@ class FlutterAbilityAndEntryDelegate implements ExclusiveAppComponent release() { this.host = null; this.flutterEngine = null; - this.textInputPlugin = undefined; this.platformPlugin = undefined; } diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyEventHandlerForEmulator.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyEventHandlerForEmulator.ets new file mode 100644 index 0000000000..34e179d714 --- /dev/null +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyEventHandlerForEmulator.ets @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { HashMap } from "@kit.ArkTS"; +import deviceInfo from '@ohos.deviceInfo'; +import TextInputPlugin from "../../plugin/editing/TextInputPlugin"; +import Log from "../../util/Log"; +import { KeyCode } from "@kit.InputKit"; + +const TAG = "KeyEventHandlerForEmulator"; + +export class KeyEventHandlerForEmulator { + private textInputPlugin?: TextInputPlugin; + private charMap : HashMap = new HashMap(); + private shiftMap : HashMap = new HashMap(); + private isShiftMode: boolean = false; + + constructor(textInputPlugin?: TextInputPlugin) { + this.textInputPlugin = textInputPlugin; + this.initCharMap(); + this.initShiftMap(); + } + + private initCharMap() { + this.charMap.set(KeyCode.KEYCODE_0, '0') + this.charMap.set(KeyCode.KEYCODE_1, '1') + this.charMap.set(KeyCode.KEYCODE_2, '2') + this.charMap.set(KeyCode.KEYCODE_3, '3') + this.charMap.set(KeyCode.KEYCODE_4, '4') + this.charMap.set(KeyCode.KEYCODE_5, '5') + this.charMap.set(KeyCode.KEYCODE_6, '6') + this.charMap.set(KeyCode.KEYCODE_7, '7') + this.charMap.set(KeyCode.KEYCODE_8, '8') + this.charMap.set(KeyCode.KEYCODE_9, '9') + this.charMap.set(KeyCode.KEYCODE_A, 'a') + this.charMap.set(KeyCode.KEYCODE_B, 'b') + this.charMap.set(KeyCode.KEYCODE_C, 'c') + this.charMap.set(KeyCode.KEYCODE_D, 'd') + this.charMap.set(KeyCode.KEYCODE_E, 'e') + this.charMap.set(KeyCode.KEYCODE_F, 'f') + this.charMap.set(KeyCode.KEYCODE_G, 'g') + this.charMap.set(KeyCode.KEYCODE_H, 'h') + this.charMap.set(KeyCode.KEYCODE_I, 'i') + this.charMap.set(KeyCode.KEYCODE_J, 'j') + this.charMap.set(KeyCode.KEYCODE_K, 'k') + this.charMap.set(KeyCode.KEYCODE_L, 'l') + this.charMap.set(KeyCode.KEYCODE_M, 'm') + this.charMap.set(KeyCode.KEYCODE_N, 'n') + this.charMap.set(KeyCode.KEYCODE_O, 'o') + this.charMap.set(KeyCode.KEYCODE_P, 'p') + this.charMap.set(KeyCode.KEYCODE_Q, 'q') + this.charMap.set(KeyCode.KEYCODE_R, 'r') + this.charMap.set(KeyCode.KEYCODE_S, 's') + this.charMap.set(KeyCode.KEYCODE_T, 't') + this.charMap.set(KeyCode.KEYCODE_U, 'u') + this.charMap.set(KeyCode.KEYCODE_V, 'v') + this.charMap.set(KeyCode.KEYCODE_W, 'w') + this.charMap.set(KeyCode.KEYCODE_X, 'x') + this.charMap.set(KeyCode.KEYCODE_Y, 'y') + this.charMap.set(KeyCode.KEYCODE_Z, 'z') + this.charMap.set(KeyCode.KEYCODE_GRAVE, '`') + this.charMap.set(KeyCode.KEYCODE_MINUS, '-') + this.charMap.set(KeyCode.KEYCODE_EQUALS, '=') + this.charMap.set(KeyCode.KEYCODE_LEFT_BRACKET, '[') + this.charMap.set(KeyCode.KEYCODE_RIGHT_BRACKET, ']') + this.charMap.set(KeyCode.KEYCODE_BACKSLASH, '\\') + this.charMap.set(KeyCode.KEYCODE_SEMICOLON, ';') + this.charMap.set(KeyCode.KEYCODE_APOSTROPHE, '\'') + this.charMap.set(KeyCode.KEYCODE_COMMA, ',') + this.charMap.set(KeyCode.KEYCODE_PERIOD, '.') + this.charMap.set(KeyCode.KEYCODE_SLASH, '/') + this.charMap.set(KeyCode.KEYCODE_SPACE, ' ') + } + + private initShiftMap() { + this.shiftMap.set(KeyCode.KEYCODE_0, ')') + this.shiftMap.set(KeyCode.KEYCODE_1, '!') + this.shiftMap.set(KeyCode.KEYCODE_2, '@') + this.shiftMap.set(KeyCode.KEYCODE_3, '#') + this.shiftMap.set(KeyCode.KEYCODE_4, '$') + this.shiftMap.set(KeyCode.KEYCODE_5, '%') + this.shiftMap.set(KeyCode.KEYCODE_6, '^') + this.shiftMap.set(KeyCode.KEYCODE_7, '&') + this.shiftMap.set(KeyCode.KEYCODE_8, '*') + this.shiftMap.set(KeyCode.KEYCODE_9, '(') + this.shiftMap.set(KeyCode.KEYCODE_A, 'A') + this.shiftMap.set(KeyCode.KEYCODE_B, 'B') + this.shiftMap.set(KeyCode.KEYCODE_C, 'C') + this.shiftMap.set(KeyCode.KEYCODE_D, 'D') + this.shiftMap.set(KeyCode.KEYCODE_E, 'E') + this.shiftMap.set(KeyCode.KEYCODE_F, 'F') + this.shiftMap.set(KeyCode.KEYCODE_G, 'G') + this.shiftMap.set(KeyCode.KEYCODE_H, 'H') + this.shiftMap.set(KeyCode.KEYCODE_I, 'I') + this.shiftMap.set(KeyCode.KEYCODE_J, 'J') + this.shiftMap.set(KeyCode.KEYCODE_K, 'K') + this.shiftMap.set(KeyCode.KEYCODE_L, 'L') + this.shiftMap.set(KeyCode.KEYCODE_M, 'M') + this.shiftMap.set(KeyCode.KEYCODE_N, 'N') + this.shiftMap.set(KeyCode.KEYCODE_O, 'O') + this.shiftMap.set(KeyCode.KEYCODE_P, 'P') + this.shiftMap.set(KeyCode.KEYCODE_Q, 'Q') + this.shiftMap.set(KeyCode.KEYCODE_R, 'R') + this.shiftMap.set(KeyCode.KEYCODE_S, 'S') + this.shiftMap.set(KeyCode.KEYCODE_T, 'T') + this.shiftMap.set(KeyCode.KEYCODE_U, 'U') + this.shiftMap.set(KeyCode.KEYCODE_V, 'V') + this.shiftMap.set(KeyCode.KEYCODE_W, 'W') + this.shiftMap.set(KeyCode.KEYCODE_X, 'X') + this.shiftMap.set(KeyCode.KEYCODE_Y, 'Y') + this.shiftMap.set(KeyCode.KEYCODE_Z, 'Z') + this.shiftMap.set(KeyCode.KEYCODE_GRAVE, '~') + this.shiftMap.set(KeyCode.KEYCODE_MINUS, '_') + this.shiftMap.set(KeyCode.KEYCODE_EQUALS, '+') + this.shiftMap.set(KeyCode.KEYCODE_LEFT_BRACKET, '{') + this.shiftMap.set(KeyCode.KEYCODE_RIGHT_BRACKET, '}') + this.shiftMap.set(KeyCode.KEYCODE_BACKSLASH, '|') + this.shiftMap.set(KeyCode.KEYCODE_SEMICOLON, ':') + this.shiftMap.set(KeyCode.KEYCODE_APOSTROPHE, '"') + this.shiftMap.set(KeyCode.KEYCODE_COMMA, '<') + this.shiftMap.set(KeyCode.KEYCODE_PERIOD, '>') + this.shiftMap.set(KeyCode.KEYCODE_SLASH, '?') + this.shiftMap.set(KeyCode.KEYCODE_SPACE, ' ') + } + + getCharByEvent(event: KeyEvent) : string { + let key = event.keyCode; + if (this.isShiftMode) { + return this.shiftMap.hasKey(key) ? this.shiftMap.get(key) : '' + } else { + return this.charMap.hasKey(key) ? this.charMap.get(key) : '' + } + } + + isEmulator() : boolean { + return deviceInfo.productModel == 'emulator'; + } + + handleKeyEvent(event: KeyEvent) { + Log.i(TAG, JSON.stringify({ + "name": "handleKeyEvent", + "event": event + })); + if (!this.isEmulator()) { + return; + } + if (event.type == KeyType.Up) { + // 处理字符按键相关逻辑 + if (this.charMap.hasKey(event.keyCode)) { + this.textInputPlugin?.getEditingState().handleInsertTextEvent(this.getCharByEvent(event)) + } + // 处理非字符按键 + if (event.keyCode == KeyCode.KEYCODE_DEL) { + this.textInputPlugin?.getEditingState().handleDeleteEvent(false, 0) + } + } + this.isShiftMode = event.keyCode == KeyCode.KEYCODE_SHIFT_LEFT + } +} diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyboardManager.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyboardManager.ets index ec4a02ff78..0951c126fb 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyboardManager.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyboardManager.ets @@ -12,14 +12,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +import TextInputPlugin from '../../plugin/editing/TextInputPlugin'; import FlutterEngine from '../engine/FlutterEngine'; import KeyEventChannel, { FlutterKeyEvent } from '../engine/systemchannels/KeyEventChannel'; +import { KeyEventHandlerForEmulator } from './KeyEventHandlerForEmulator'; export default class KeyboardManager { private keyEventChannel: KeyEventChannel | null = null + private keyEventHandlerForEmulator: KeyEventHandlerForEmulator; - constructor(engine: FlutterEngine) { + constructor(engine: FlutterEngine, textInputPlugin: TextInputPlugin) { this.keyEventChannel = new KeyEventChannel(engine.dartExecutor) + this.keyEventHandlerForEmulator = new KeyEventHandlerForEmulator(textInputPlugin); } handleKeyEvent(event: KeyEvent) { @@ -28,5 +32,6 @@ export default class KeyboardManager { } }) + this.keyEventHandlerForEmulator.handleKeyEvent(event); } } \ No newline at end of file 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 c64f1d88f9..5f3a67c362 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 @@ -46,6 +46,10 @@ export default class TextInputPlugin implements EditingStateWatcher { } + getEditingState() { + return this.mTextInputHandler.mEditable; + } + didChangeEditingState(textChanged: boolean, selectionChanged: boolean, composingRegionChanged: boolean): void { let editable = this.mTextInputHandler.mEditable; let inputTarget = this.mTextInputHandler.inputTarget; @@ -68,6 +72,10 @@ export default class TextInputPlugin implements EditingStateWatcher { } }) } + + destroy() { + this.textInputChannel.setTextInputMethodHandler(null); + } } class TextInputMethodHandlerImpl implements TextInputMethodHandler { diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets index 5af7b9cf9d..aeb9518b6a 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets @@ -25,6 +25,7 @@ import ArrayList from '@ohos.util.ArrayList'; import { EmbeddingNodeController } from '../embedding/ohos/EmbeddingNodeController'; import PlatformView, { Params } from '../plugin/platform/PlatformView'; import { JSON } from '@kit.ArkTS'; +import TextInputPlugin from '../plugin/editing/TextInputPlugin'; const TAG = "FlutterViewTag"; @@ -120,6 +121,7 @@ export class FlutterView { private keyboardManager: KeyboardManager | null = null; private mainWindow: window.Window | null = null; private mouseCursorPlugin?: MouseCursorPlugin; + private textInputPlugin?: TextInputPlugin; private uiContext?: UIContext | undefined; private settings?: Settings; private mFirstFrameListeners: ArrayList; @@ -285,7 +287,6 @@ export class FlutterView { } Log.i(TAG, "attachToFlutterEngine"); this.flutterEngine = flutterEngine; - this.keyboardManager = new KeyboardManager(flutterEngine); if (this.isSurfaceAvailableForRendering) { this.flutterEngine.getFlutterNapi().xComponentAttachFlutterEngine(this.id) } @@ -306,6 +307,8 @@ export class FlutterView { let windowId = this.mainWindow?.getWindowProperties()?.id ?? 0 this.mouseCursorPlugin = new MouseCursorPlugin(windowId, this.flutterEngine?.getMouseCursorChannel()!); + this.textInputPlugin = new TextInputPlugin(this.flutterEngine?.getTextInputChannel()!); + this.keyboardManager = new KeyboardManager(flutterEngine, this.textInputPlugin!); this.settings = new Settings(this.flutterEngine.getSettingsChannel()!); this.sendSettings(); this.isFlutterUiDisplayed = this.flutterEngine.getFlutterNapi().isDisplayingFlutterUi; @@ -327,6 +330,7 @@ export class FlutterView { this.flutterEngine?.getPlatformViewsController()?.detachFromView(); this.flutterEngine = null; this.keyboardManager = null; + this.textInputPlugin?.destroy(); } onWindowCreated() { -- Gitee From e63bf0e8c433b86225c87f9441288b9832a721ba Mon Sep 17 00:00:00 2001 From: hezhengyi Date: Mon, 11 Nov 2024 15:11:33 +0800 Subject: [PATCH 2/2] =?UTF-8?q?KeyEvent=E4=BA=8B=E4=BB=B6=E5=A4=84?= =?UTF-8?q?=E7=90=86=E9=80=BB=E8=BE=91=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hezhengyi --- shell/platform/ohos/flutter_embedding/.gitignore | 2 +- .../src/main/ets/embedding/ohos/FlutterPage.ets | 14 ++++++++------ ...HandlerForEmulator.ets => KeyEventHandler.ets} | 11 ++--------- .../main/ets/embedding/ohos/KeyboardManager.ets | 15 ++++++++++----- .../flutter/src/main/ets/view/FlutterView.ets | 8 ++++++-- 5 files changed, 27 insertions(+), 23 deletions(-) rename shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/{KeyEventHandlerForEmulator.ets => KeyEventHandler.ets} (96%) diff --git a/shell/platform/ohos/flutter_embedding/.gitignore b/shell/platform/ohos/flutter_embedding/.gitignore index c5c48a55d3..c837164ae8 100644 --- a/shell/platform/ohos/flutter_embedding/.gitignore +++ b/shell/platform/ohos/flutter_embedding/.gitignore @@ -1,5 +1,5 @@ /node_modules -/oh_modules +**/oh_modules /.idea **/build /.hvigor diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterPage.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterPage.ets index 15af51a2dd..ddb2fbec75 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterPage.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/FlutterPage.ets @@ -80,9 +80,10 @@ export struct FlutterPage { } }) .onKeyPreIme((event: KeyEvent) => { - Log.d(TAG, "onKeyEvent " + event.type); - this.flutterView?.onKeyEvent(event); - return false; + return this.flutterView?.onKeyPreIme(event) ?? false; + }) + .onKeyEvent((event: KeyEvent) => { + return this.flutterView?.onKeyEvent(event) ?? false; }) } @Builder mouseWheelPage() { @@ -134,9 +135,10 @@ export struct FlutterPage { } }) .onKeyPreIme((event: KeyEvent) => { - Log.d(TAG, "onKeyEvent " + event.type); - this.flutterView?.onKeyEvent(event); - return false; + return this.flutterView?.onKeyPreIme(event) ?? false; + }) + .onKeyEvent((event: KeyEvent) => { + return this.flutterView?.onKeyEvent(event) ?? false; }) .gesture( PanGesture(this.panOption) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyEventHandlerForEmulator.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyEventHandler.ets similarity index 96% rename from shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyEventHandlerForEmulator.ets rename to shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyEventHandler.ets index 34e179d714..58133fd461 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyEventHandlerForEmulator.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyEventHandler.ets @@ -19,9 +19,9 @@ import TextInputPlugin from "../../plugin/editing/TextInputPlugin"; import Log from "../../util/Log"; import { KeyCode } from "@kit.InputKit"; -const TAG = "KeyEventHandlerForEmulator"; +const TAG = "KeyEventHandler"; -export class KeyEventHandlerForEmulator { +export class KeyEventHandler { private textInputPlugin?: TextInputPlugin; private charMap : HashMap = new HashMap(); private shiftMap : HashMap = new HashMap(); @@ -144,18 +144,11 @@ export class KeyEventHandlerForEmulator { } } - isEmulator() : boolean { - return deviceInfo.productModel == 'emulator'; - } - handleKeyEvent(event: KeyEvent) { Log.i(TAG, JSON.stringify({ "name": "handleKeyEvent", "event": event })); - if (!this.isEmulator()) { - return; - } if (event.type == KeyType.Up) { // 处理字符按键相关逻辑 if (this.charMap.hasKey(event.keyCode)) { diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyboardManager.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyboardManager.ets index 0951c126fb..21b5c88934 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyboardManager.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyboardManager.ets @@ -15,23 +15,28 @@ import TextInputPlugin from '../../plugin/editing/TextInputPlugin'; import FlutterEngine from '../engine/FlutterEngine'; import KeyEventChannel, { FlutterKeyEvent } from '../engine/systemchannels/KeyEventChannel'; -import { KeyEventHandlerForEmulator } from './KeyEventHandlerForEmulator'; +import { KeyEventHandler } from './KeyEventHandler'; export default class KeyboardManager { private keyEventChannel: KeyEventChannel | null = null - private keyEventHandlerForEmulator: KeyEventHandlerForEmulator; + private keyEventHandler: KeyEventHandler; constructor(engine: FlutterEngine, textInputPlugin: TextInputPlugin) { this.keyEventChannel = new KeyEventChannel(engine.dartExecutor) - this.keyEventHandlerForEmulator = new KeyEventHandlerForEmulator(textInputPlugin); + this.keyEventHandler = new KeyEventHandler(textInputPlugin); } - handleKeyEvent(event: KeyEvent) { + onKeyPreIme(event: KeyEvent) : boolean { this.keyEventChannel?.sendFlutterKeyEvent(new FlutterKeyEvent(event), event.type == KeyType.Up, { onFrameworkResponse: (isEventHandled: boolean): void => { } }) - this.keyEventHandlerForEmulator.handleKeyEvent(event); + return false; + } + + onKeyEvent(event: KeyEvent) : boolean { + this.keyEventHandler.handleKeyEvent(event); + return false; } } \ No newline at end of file diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets index aeb9518b6a..853ce08722 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/view/FlutterView.ets @@ -452,8 +452,12 @@ export class FlutterView { return false; } - onKeyEvent(event: KeyEvent) { - this.keyboardManager?.handleKeyEvent(event) + onKeyPreIme(event: KeyEvent) : boolean { + return this.keyboardManager?.onKeyPreIme(event) ?? false; + } + + onKeyEvent(event: KeyEvent) : boolean { + return this.keyboardManager?.onKeyEvent(event) ?? false; } onMouseWheel(eventType: string, event: PanGestureEvent) { -- Gitee