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 58133fd461d8669580f47c2358ae33739957c70d..5fd76b3d62429eebe9345305e7b59f95f8abb0ec 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,20 @@ 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; @@ -147,18 +156,30 @@ export class KeyEventHandler { handleKeyEvent(event: KeyEvent) { Log.i(TAG, JSON.stringify({ "name": "handleKeyEvent", - "event": event + "event": event, })); - if (event.type == KeyType.Up) { - // 处理字符按键相关逻辑 - if (this.charMap.hasKey(event.keyCode)) { - this.textInputPlugin?.getEditingState().handleInsertTextEvent(this.getCharByEvent(event)) + let text = this.getCharByEvent(event); + if (event.type == KeyType.Down) { + if (!this.isCombinationKey) { + this.isCombinationKey = COMBINATION_KEYS.findIndex((it) => it == event.keyCode) >= 0; } - // 处理非字符按键 - if (event.keyCode == KeyCode.KEYCODE_DEL) { - this.textInputPlugin?.getEditingState().handleDeleteEvent(false, 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 (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 + || 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 351277019b2ba94b0b7cd759d33574c48b0b3198..42f1cec3c5bab24dc157117d50b849caf42138ef 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; diff --git a/shell/platform/ohos/ohos_touch_processor.cpp b/shell/platform/ohos/ohos_touch_processor.cpp index 32d2ed1ef8d0f420c4c0af25ca01194e299d1923..17b715b32823346890122a73f119a4593a207b00 100644 --- a/shell/platform/ohos/ohos_touch_processor.cpp +++ b/shell/platform/ohos/ohos_touch_processor.cpp @@ -179,11 +179,9 @@ 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; - } + if (pointerData.change == PointerData::Change::kDown || + pointerData.change == PointerData::Change::kMove) { + pointerData.buttons = kPointerButtonTouchContact; } pointerData.pan_x = 0.0; pointerData.pan_y = 0.0; @@ -261,7 +259,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 0d1f9360237e1adff77bf0880e2abe7519af5bca..6067b74b5d5d224e0b7a80ea4b0797325e1c2f7e 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;