Fix/android check normal usbhid usage (#9784)

* fix: android check normal usbhid usage

Signed-off-by: fufesou <linlong1266@gmail.com>

* fix: android input, ignore composing if is deleting

Signed-off-by: fufesou <linlong1266@gmail.com>

---------

Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
fufesou 2024-10-30 15:29:52 +08:00 committed by GitHub
parent 711ed28846
commit bae4a2c710
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 20 deletions

View File

@ -224,7 +224,9 @@ class _RemotePageState extends State<RemotePage> {
void _handleNonIOSSoftKeyboardInput(String newValue) { void _handleNonIOSSoftKeyboardInput(String newValue) {
_lastComposingChangeValid = _textController.value.isComposingRangeValid; _lastComposingChangeValid = _textController.value.isComposingRangeValid;
if (_lastComposingChangeValid) { if (_lastComposingChangeValid && newValue.length > _value.length) {
// Only early return if is composing new words.
// We need to send `backspace` immediately if is deleting letters.
return; return;
} }
var oldValue = _value; var oldValue = _value;

View File

@ -544,26 +544,40 @@ class InputModel {
handleKeyDownEventModifiers(e); handleKeyDownEventModifiers(e);
} }
// The physicalKey.usbHidUsage may be not correct for soft keyboard on Android. bool isMobileAndMapMode = false;
// iOS does not have this issue. if (isMobile) {
// 1. Open the soft keyboard on Android // Do not use map mode if mobile -> Android. Android does not support map mode for now.
// 2. Switch to input method like zh/ko/ja // Because simulating the physical key events(uhid) which requires root permission is not supported.
// 3. Click Backspace and Enter on the soft keyboard or physical keyboard if (peerPlatform != kPeerPlatformAndroid) {
// 4. The physicalKey.usbHidUsage is not correct. if (isIOS) {
// PhysicalKeyboardKey#8ac83(usbHidUsage: "0x1100000042", debugName: "Key with ID 0x1100000042") isMobileAndMapMode = true;
// LogicalKeyboardKey#2604c(keyId: "0x10000000d", keyLabel: "Enter", debugName: "Enter") } else {
// // The physicalKey.usbHidUsage may be not correct for soft keyboard on Android.
// The correct PhysicalKeyboardKey should be // iOS does not have this issue.
// PhysicalKeyboardKey#e14a9(usbHidUsage: "0x00070028", debugName: "Enter") // 1. Open the soft keyboard on Android
// https://github.com/flutter/flutter/issues/157771 // 2. Switch to input method like zh/ko/ja
// We cannot use the debugName to determine the key is correct or not, because it's null in release mode. // 3. Click Backspace and Enter on the soft keyboard or physical keyboard
// to-do: `isLegacyModeKeys` is not the best workaround, we need to find a better way to fix this issue. // 4. The physicalKey.usbHidUsage is not correct.
final isLegacyModeKeys = ['Backspace', 'Enter'].contains(e.logicalKey.keyLabel); // PhysicalKeyboardKey#8ac83(usbHidUsage: "0x1100000042", debugName: "Key with ID 0x1100000042")
final isMobileAndPeerNotAndroid = // LogicalKeyboardKey#2604c(keyId: "0x10000000d", keyLabel: "Enter", debugName: "Enter")
isMobile && peerPlatform != kPeerPlatformAndroid; //
// The correct PhysicalKeyboardKey should be
// PhysicalKeyboardKey#e14a9(usbHidUsage: "0x00070028", debugName: "Enter")
// https://github.com/flutter/flutter/issues/157771
// We cannot use the debugName to determine the key is correct or not, because it's null in release mode.
// The normal `usbHidUsage` for keyboard shoud be between [0x00000010, 0x000c029f]
// https://github.com/flutter/flutter/blob/c051b69e2a2224300e20d93dbd15f4b91e8844d1/packages/flutter/lib/src/services/keyboard_key.g.dart#L5332 - 5600
final isNormalHsbHidUsage = (e.physicalKey.usbHidUsage >> 20) == 0;
isMobileAndMapMode = isNormalHsbHidUsage &&
// No need to check `!['Backspace', 'Enter'].contains(e.logicalKey.keyLabel)`
// But we still add it for more reliability.
!['Backspace', 'Enter'].contains(e.logicalKey.keyLabel);
}
}
}
final isDesktopAndMapMode = final isDesktopAndMapMode =
isDesktop || isWebDesktop && keyboardMode == kKeyMapMode; isDesktop || (isWebDesktop && keyboardMode == kKeyMapMode);
if (!isLegacyModeKeys && (isMobileAndPeerNotAndroid || isDesktopAndMapMode)) { if (isMobileAndMapMode || isDesktopAndMapMode) {
// FIXME: e.character is wrong for dead keys, eg: ^ in de // FIXME: e.character is wrong for dead keys, eg: ^ in de
newKeyboardMode( newKeyboardMode(
e.character ?? '', e.character ?? '',