From 19e0caa05eb0d0d770cffe750bc448f966daa927 Mon Sep 17 00:00:00 2001 From: hezhengyi Date: Sat, 7 Dec 2024 15:22:25 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E9=80=82=E9=85=8D2in1,=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E9=BC=A0=E6=A0=87=E6=A1=86=E9=80=89=E6=96=87=E5=AD=97,?= =?UTF-8?q?=E8=A7=84=E9=81=BF=E7=BB=84=E5=90=88=E9=94=AE=E7=9A=84=E5=86=B2?= =?UTF-8?q?=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hezhengyi --- .../ets/embedding/ohos/KeyEventHandler.ets | 20 ++++++++++++++++++- shell/platform/ohos/ohos_touch_processor.cpp | 9 ++------- .../platform/ohos/ohos_xcomponent_adapter.cpp | 2 +- 3 files changed, 22 insertions(+), 9 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 58133fd461..df38b7093b 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,11 +21,17 @@ import { KeyCode } from "@kit.InputKit"; const TAG = "KeyEventHandler"; +const CONTROL_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 isControlMode: boolean = false; constructor(textInputPlugin?: TextInputPlugin) { this.textInputPlugin = textInputPlugin; @@ -147,9 +153,20 @@ export class KeyEventHandler { handleKeyEvent(event: KeyEvent) { Log.i(TAG, JSON.stringify({ "name": "handleKeyEvent", - "event": event + "event": event, })); + if (event.type == KeyType.Down) { + if (!this.isControlMode) { + this.isControlMode = CONTROL_KEYS.findIndex((it) => it == event.keyCode) >= 0; + } + } + Log.i(TAG, "isControlMode=" + this.isControlMode) if (event.type == KeyType.Up) { + if (!this.isControlMode) { + this.isControlMode = CONTROL_KEYS.findIndex((it) => it == event.keyCode) >= 0; + } else { + return; + } // 处理字符按键相关逻辑 if (this.charMap.hasKey(event.keyCode)) { this.textInputPlugin?.getEditingState().handleInsertTextEvent(this.getCharByEvent(event)) @@ -160,5 +177,6 @@ export class KeyEventHandler { } } this.isShiftMode = event.keyCode == KeyCode.KEYCODE_SHIFT_LEFT + || event.keyCode == KeyCode.KEYCODE_SHIFT_RIGHT } } diff --git a/shell/platform/ohos/ohos_touch_processor.cpp b/shell/platform/ohos/ohos_touch_processor.cpp index 32d2ed1ef8..b820d49e4a 100644 --- a/shell/platform/ohos/ohos_touch_processor.cpp +++ b/shell/platform/ohos/ohos_touch_processor.cpp @@ -179,12 +179,7 @@ void OhosTouchProcessor::HandleTouchEvent( OH_NativeXComponent_TouchPointToolType toolType; OH_NativeXComponent_GetTouchPointToolType(component, 0, &toolType); pointerData.kind = getPointerDeviceTypeForToolType(toolType); - if (pointerData.kind == PointerData::DeviceKind::kTouch) { - if (pointerData.change == PointerData::Change::kDown || - pointerData.change == PointerData::Change::kMove) { - pointerData.buttons = kPointerButtonTouchContact; - } - } + pointerData.buttons = kPointerButtonTouchContact; pointerData.pan_x = 0.0; pointerData.pan_y = 0.0; // Delta will be generated in pointer_data_packet_converter.cc. @@ -261,7 +256,7 @@ void OhosTouchProcessor::HandleMouseEvent( pointerData.pressure = 0.0; pointerData.pressure_max = 1.0; pointerData.pressure_min = 0.0; - pointerData.kind = PointerData::DeviceKind::kTouch; + pointerData.kind = PointerData::DeviceKind::kMouse; // kMouse支持鼠标框选文字 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 0d1f936023..7f6fc75a1e 100644 --- a/shell/platform/ohos/ohos_xcomponent_adapter.cpp +++ b/shell/platform/ohos/ohos_xcomponent_adapter.cpp @@ -528,7 +528,7 @@ void XComponentBase::OnDispatchMouseWheelEvent(mouseWheelEvent event) } if (event.eventType == "actionUpdate") { OH_NativeXComponent_MouseEvent mouseEvent; - double scrollY = event.offsetY - g_scrollDistance; + double scrollY = -(event.offsetY - g_scrollDistance); g_scrollDistance = event.offsetY; // fix resize ratio mouseEvent.x = event.globalX / g_resizeRate; -- Gitee From 669ad6145fe5b8189a22bf139ca2d4ff5d4649cb Mon Sep 17 00:00:00 2001 From: hezhengyi Date: Thu, 12 Dec 2024 11:31:35 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E4=B8=8D=E5=A4=84=E7=90=86=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E9=94=AE=E7=9A=84keyEvent=E4=BA=8B=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hezhengyi --- .../flutter/src/main/ets/embedding/ohos/KeyEventHandler.ets | 4 ---- 1 file changed, 4 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 df38b7093b..0c9dadf394 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 @@ -171,10 +171,6 @@ export class KeyEventHandler { 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 -- Gitee From b298be8bd267fbd46fbb7cebf361bcfb8e817649 Mon Sep 17 00:00:00 2001 From: hezhengyi Date: Thu, 19 Dec 2024 16:39:32 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9ctrl=E7=BB=84=E5=90=88?= =?UTF-8?q?=E9=94=AE=E7=9A=84=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: hezhengyi --- .../ets/embedding/ohos/KeyEventHandler.ets | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 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 0c9dadf394..3aff0a47bc 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 @@ -32,6 +32,8 @@ export class KeyEventHandler { private shiftMap : HashMap = new HashMap(); private isShiftMode: boolean = false; private isControlMode: boolean = false; + // 记录输入的keyCode,确保有down和up事件才输入字符 + private inputMap: HashMap = new HashMap(); constructor(textInputPlugin?: TextInputPlugin) { this.textInputPlugin = textInputPlugin; @@ -155,21 +157,25 @@ export class KeyEventHandler { "name": "handleKeyEvent", "event": event, })); + let text = this.getCharByEvent(event); if (event.type == KeyType.Down) { if (!this.isControlMode) { this.isControlMode = CONTROL_KEYS.findIndex((it) => it == event.keyCode) >= 0; } - } - Log.i(TAG, "isControlMode=" + this.isControlMode) - if (event.type == KeyType.Up) { - if (!this.isControlMode) { - this.isControlMode = CONTROL_KEYS.findIndex((it) => it == event.keyCode) >= 0; - } else { + this.inputMap.set(event.keyCode, text); + } else if (event.type == KeyType.Up) { + if (CONTROL_KEYS.findIndex((it) => it == event.keyCode) >= 0) { + // Ctrl/Alt 键抬起,重置状态 + this.isControlMode = false; + return; + } else if (this.isControlMode) { + // Ctrl/Alt 键按下的状态,不输入字符(字母/数字/符号) return; } // 处理字符按键相关逻辑 - if (this.charMap.hasKey(event.keyCode)) { - this.textInputPlugin?.getEditingState().handleInsertTextEvent(this.getCharByEvent(event)) + if (this.inputMap.hasKey(event.keyCode) && this.charMap.hasKey(event.keyCode)) { + this.inputMap.remove(event.keyCode) + this.textInputPlugin?.getEditingState().handleInsertTextEvent(text) } } this.isShiftMode = event.keyCode == KeyCode.KEYCODE_SHIFT_LEFT -- Gitee From 0698ca6ca6df3be69fc775bf6ef7530c4f4f5e0f Mon Sep 17 00:00:00 2001 From: hezhengyi Date: Tue, 24 Dec 2024 17:38:37 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=A3=80=E8=A7=86=E6=84=8F=E8=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: hezhengyi --- .../main/ets/embedding/ohos/KeyEventHandler.ets | 15 ++++++++------- shell/platform/ohos/ohos_touch_processor.cpp | 5 ++++- shell/platform/ohos/ohos_xcomponent_adapter.cpp | 3 ++- 3 files changed, 14 insertions(+), 9 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 3aff0a47bc..5fd76b3d62 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,7 +21,8 @@ import { KeyCode } from "@kit.InputKit"; const TAG = "KeyEventHandler"; -const CONTROL_KEYS = [ +// 组合键 +const COMBINATION_KEYS = [ KeyCode.KEYCODE_CTRL_LEFT, KeyCode.KEYCODE_CTRL_RIGHT, KeyCode.KEYCODE_ALT_LEFT, KeyCode.KEYCODE_ALT_RIGHT ]; @@ -31,7 +32,7 @@ export class KeyEventHandler { private charMap : HashMap = new HashMap(); private shiftMap : HashMap = new HashMap(); private isShiftMode: boolean = false; - private isControlMode: boolean = false; + private isCombinationKey: boolean = false; // 记录输入的keyCode,确保有down和up事件才输入字符 private inputMap: HashMap = new HashMap(); @@ -159,16 +160,16 @@ export class KeyEventHandler { })); let text = this.getCharByEvent(event); if (event.type == KeyType.Down) { - if (!this.isControlMode) { - this.isControlMode = CONTROL_KEYS.findIndex((it) => it == event.keyCode) >= 0; + 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 (CONTROL_KEYS.findIndex((it) => it == event.keyCode) >= 0) { + if (COMBINATION_KEYS.findIndex((it) => it == event.keyCode) >= 0) { // Ctrl/Alt 键抬起,重置状态 - this.isControlMode = false; + this.isCombinationKey = false; return; - } else if (this.isControlMode) { + } else if (this.isCombinationKey) { // Ctrl/Alt 键按下的状态,不输入字符(字母/数字/符号) return; } diff --git a/shell/platform/ohos/ohos_touch_processor.cpp b/shell/platform/ohos/ohos_touch_processor.cpp index b820d49e4a..17b715b328 100644 --- a/shell/platform/ohos/ohos_touch_processor.cpp +++ b/shell/platform/ohos/ohos_touch_processor.cpp @@ -179,7 +179,10 @@ void OhosTouchProcessor::HandleTouchEvent( OH_NativeXComponent_TouchPointToolType toolType; OH_NativeXComponent_GetTouchPointToolType(component, 0, &toolType); pointerData.kind = getPointerDeviceTypeForToolType(toolType); - pointerData.buttons = kPointerButtonTouchContact; + if (pointerData.change == PointerData::Change::kDown || + pointerData.change == PointerData::Change::kMove) { + pointerData.buttons = kPointerButtonTouchContact; + } pointerData.pan_x = 0.0; pointerData.pan_y = 0.0; // Delta will be generated in pointer_data_packet_converter.cc. diff --git a/shell/platform/ohos/ohos_xcomponent_adapter.cpp b/shell/platform/ohos/ohos_xcomponent_adapter.cpp index 7f6fc75a1e..6067b74b5d 100644 --- a/shell/platform/ohos/ohos_xcomponent_adapter.cpp +++ b/shell/platform/ohos/ohos_xcomponent_adapter.cpp @@ -528,7 +528,8 @@ void XComponentBase::OnDispatchMouseWheelEvent(mouseWheelEvent event) } if (event.eventType == "actionUpdate") { OH_NativeXComponent_MouseEvent mouseEvent; - double scrollY = -(event.offsetY - g_scrollDistance); + // 调整鼠标滚轮滚动时,列表滑动的方向。和Windows保持一致。 + double scrollY = g_scrollDistance - event.offsetY; g_scrollDistance = event.offsetY; // fix resize ratio mouseEvent.x = event.globalX / g_resizeRate; -- Gitee From fc5c3c651745f8ee5d1efcd789ea43a9c40910ef Mon Sep 17 00:00:00 2001 From: hezhengyi Date: Thu, 26 Dec 2024 16:45:26 +0800 Subject: [PATCH 5/5] Using length in handleDeleteEvent. cherry pick from 3.22 e1fb699632330a5222699d9012eecfc59dcd39dc Signed-off-by: hezhengyi --- .../plugin/editing/ListenableEditingState.ets | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) 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 351277019b..42f1cec3c5 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,7 +253,12 @@ export class ListenableEditingState { } handleDeleteEvent(leftOrRight: boolean, length: number): void { - let start = this.mSelectionStartCache < this.mSelectionEndCache ? this.mSelectionStartCache : this.mSelectionEndCache; + if (length === 0) { + return; + } + + let start = + this.mSelectionStartCache < this.mSelectionEndCache ? this.mSelectionStartCache : this.mSelectionEndCache; let end = this.mSelectionStartCache > this.mSelectionEndCache ? this.mSelectionStartCache : this.mSelectionEndCache; if (leftOrRight == false) { @@ -261,9 +266,15 @@ export class ListenableEditingState { if (start == 0 && end == 0) { return; } + let unicodeStart = start; if (start == end) { - unicodeStart = FlutterTextUtils.getOffsetBefore(this.mStringCache, start); + for (let i = 0; i < length; i++) { + unicodeStart = FlutterTextUtils.getOffsetBefore(this.mStringCache, unicodeStart); + if (unicodeStart === 0) { + break; + } + } } this.replace(unicodeStart, end, "", 0, 0); this.mSelectionStartCache = unicodeStart; @@ -277,7 +288,12 @@ export class ListenableEditingState { } let unicodeEnd = end; if (start == end) { - unicodeEnd = FlutterTextUtils.getOffsetAfter(this.mStringCache, start); + for (let i = 0; i < length; i++) { + unicodeEnd = FlutterTextUtils.getOffsetAfter(this.mStringCache, unicodeEnd); + if (unicodeEnd === this.mStringCache.length) { + break; + } + } } this.replace(start, unicodeEnd, "", 0, 0); this.mSelectionEndCache = start; -- Gitee