From bb6439e6c69f0a64cc58422e3e64be2ef99854d9 Mon Sep 17 00:00:00 2001 From: gxzmf <279822581@qq.com> Date: Wed, 25 Dec 2024 14:44:59 +0000 Subject: [PATCH] =?UTF-8?q?=E5=9B=9E=E9=80=80=20'Pull=20Request=20!567=20:?= =?UTF-8?q?=20=E9=80=82=E9=85=8D2in1=EF=BC=8C=E4=BF=AE=E6=94=B9=E9=BC=A0?= =?UTF-8?q?=E6=A0=87=E6=BB=9A=E8=BD=AE=E6=96=B9=E5=90=91=E5=92=8CkeyEvent?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gxzmf <279822581@qq.com> --- .../ets/embedding/ohos/KeyEventHandler.ets | 37 ++++--------------- .../plugin/editing/ListenableEditingState.ets | 22 ++--------- .../ets/plugin/editing/TextInputPlugin.ets | 8 ---- shell/platform/ohos/ohos_touch_processor.cpp | 10 +++-- .../platform/ohos/ohos_xcomponent_adapter.cpp | 3 +- 5 files changed, 18 insertions(+), 62 deletions(-) diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyEventHandler.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyEventHandler.ets index 5fd76b3d62..58133fd461 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyEventHandler.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/ohos/KeyEventHandler.ets @@ -21,20 +21,11 @@ import { KeyCode } from "@kit.InputKit"; const TAG = "KeyEventHandler"; -// 组合键 -const COMBINATION_KEYS = [ - KeyCode.KEYCODE_CTRL_LEFT, KeyCode.KEYCODE_CTRL_RIGHT, - KeyCode.KEYCODE_ALT_LEFT, KeyCode.KEYCODE_ALT_RIGHT -]; - export class KeyEventHandler { private textInputPlugin?: TextInputPlugin; private charMap : HashMap = new HashMap(); private shiftMap : HashMap = new HashMap(); private isShiftMode: boolean = false; - private isCombinationKey: boolean = false; - // 记录输入的keyCode,确保有down和up事件才输入字符 - private inputMap: HashMap = new HashMap(); constructor(textInputPlugin?: TextInputPlugin) { this.textInputPlugin = textInputPlugin; @@ -156,30 +147,18 @@ export class KeyEventHandler { handleKeyEvent(event: KeyEvent) { Log.i(TAG, JSON.stringify({ "name": "handleKeyEvent", - "event": event, + "event": event })); - let text = this.getCharByEvent(event); - if (event.type == KeyType.Down) { - if (!this.isCombinationKey) { - this.isCombinationKey = COMBINATION_KEYS.findIndex((it) => it == event.keyCode) >= 0; - } - this.inputMap.set(event.keyCode, text); - } else if (event.type == KeyType.Up) { - if (COMBINATION_KEYS.findIndex((it) => it == event.keyCode) >= 0) { - // Ctrl/Alt 键抬起,重置状态 - this.isCombinationKey = false; - return; - } else if (this.isCombinationKey) { - // Ctrl/Alt 键按下的状态,不输入字符(字母/数字/符号) - return; - } + if (event.type == KeyType.Up) { // 处理字符按键相关逻辑 - if (this.inputMap.hasKey(event.keyCode) && this.charMap.hasKey(event.keyCode)) { - this.inputMap.remove(event.keyCode) - this.textInputPlugin?.getEditingState().handleInsertTextEvent(text) + 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 - || event.keyCode == KeyCode.KEYCODE_SHIFT_RIGHT } } diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/ListenableEditingState.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/ListenableEditingState.ets index 42f1cec3c5..351277019b 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/ListenableEditingState.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/ListenableEditingState.ets @@ -253,12 +253,7 @@ export class ListenableEditingState { } handleDeleteEvent(leftOrRight: boolean, length: number): void { - if (length === 0) { - return; - } - - let start = - this.mSelectionStartCache < this.mSelectionEndCache ? this.mSelectionStartCache : this.mSelectionEndCache; + let start = this.mSelectionStartCache < this.mSelectionEndCache ? this.mSelectionStartCache : this.mSelectionEndCache; let end = this.mSelectionStartCache > this.mSelectionEndCache ? this.mSelectionStartCache : this.mSelectionEndCache; if (leftOrRight == false) { @@ -266,15 +261,9 @@ export class ListenableEditingState { if (start == 0 && end == 0) { return; } - let unicodeStart = start; if (start == end) { - for (let i = 0; i < length; i++) { - unicodeStart = FlutterTextUtils.getOffsetBefore(this.mStringCache, unicodeStart); - if (unicodeStart === 0) { - break; - } - } + unicodeStart = FlutterTextUtils.getOffsetBefore(this.mStringCache, start); } this.replace(unicodeStart, end, "", 0, 0); this.mSelectionStartCache = unicodeStart; @@ -288,12 +277,7 @@ export class ListenableEditingState { } let unicodeEnd = end; if (start == end) { - for (let i = 0; i < length; i++) { - unicodeEnd = FlutterTextUtils.getOffsetAfter(this.mStringCache, unicodeEnd); - if (unicodeEnd === this.mStringCache.length) { - break; - } - } + unicodeEnd = FlutterTextUtils.getOffsetAfter(this.mStringCache, start); } this.replace(start, unicodeEnd, "", 0, 0); this.mSelectionEndCache = start; 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 0b7fd575a4..50be54c4b0 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 @@ -307,14 +307,6 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { } private deleteLeftCallback = (length: number) => { - /// 暂时规避方案 - /// OS机制与Android不一致,需要去监听软键盘事件才能发送KeyEvent事件 - if (this.mEditable.getStringCache() == "") { - for (let i = 0; i < length; i++) { - this.sendKeyboardEvent(KeyType.Down, KeyCode.KEYCODE_DEL) - this.sendKeyboardEvent(KeyType.Up, KeyCode.KEYCODE_DEL) - } - } this.mEditable.handleDeleteEvent(false, length); } diff --git a/shell/platform/ohos/ohos_touch_processor.cpp b/shell/platform/ohos/ohos_touch_processor.cpp index 17b715b328..32d2ed1ef8 100644 --- a/shell/platform/ohos/ohos_touch_processor.cpp +++ b/shell/platform/ohos/ohos_touch_processor.cpp @@ -179,9 +179,11 @@ void OhosTouchProcessor::HandleTouchEvent( OH_NativeXComponent_TouchPointToolType toolType; OH_NativeXComponent_GetTouchPointToolType(component, 0, &toolType); pointerData.kind = getPointerDeviceTypeForToolType(toolType); - if (pointerData.change == PointerData::Change::kDown || - pointerData.change == PointerData::Change::kMove) { - pointerData.buttons = kPointerButtonTouchContact; + if (pointerData.kind == PointerData::DeviceKind::kTouch) { + if (pointerData.change == PointerData::Change::kDown || + pointerData.change == PointerData::Change::kMove) { + pointerData.buttons = kPointerButtonTouchContact; + } } pointerData.pan_x = 0.0; pointerData.pan_y = 0.0; @@ -259,7 +261,7 @@ void OhosTouchProcessor::HandleMouseEvent( pointerData.pressure = 0.0; pointerData.pressure_max = 1.0; pointerData.pressure_min = 0.0; - pointerData.kind = PointerData::DeviceKind::kMouse; // kMouse支持鼠标框选文字 + pointerData.kind = PointerData::DeviceKind::kTouch; pointerData.buttons = getPointerButtonFromMouse(mouseEvent.button); // hover support if (mouseEvent.button == OH_NATIVEXCOMPONENT_NONE_BUTTON && pointerData.change == PointerData::Change::kMove) { diff --git a/shell/platform/ohos/ohos_xcomponent_adapter.cpp b/shell/platform/ohos/ohos_xcomponent_adapter.cpp index 6067b74b5d..0d1f936023 100644 --- a/shell/platform/ohos/ohos_xcomponent_adapter.cpp +++ b/shell/platform/ohos/ohos_xcomponent_adapter.cpp @@ -528,8 +528,7 @@ void XComponentBase::OnDispatchMouseWheelEvent(mouseWheelEvent event) } if (event.eventType == "actionUpdate") { OH_NativeXComponent_MouseEvent mouseEvent; - // 调整鼠标滚轮滚动时,列表滑动的方向。和Windows保持一致。 - double scrollY = g_scrollDistance - event.offsetY; + double scrollY = event.offsetY - g_scrollDistance; g_scrollDistance = event.offsetY; // fix resize ratio mouseEvent.x = event.globalX / g_resizeRate; -- Gitee