[Bug]: Android Character Input Loss Bug Report
### Version
```sh
## Environment
- Package: `@midscene/android`
- Version: 1.5.0
- Platform: Android automation via ADB
```
### Details
# Android Character Input Loss Bug Report
## Overview
On Android devices, due to the lack of delay in native adb input, some characters are randomly lost during input. For example, when trying to input "310110197211165036", sometimes only "310019165036" is entered.
## Steps to Reproduce
Try inputting a long numeric string, such as an ID number:
```typescript
await device.keyboardType('310110197211111265036');
```
Observe that characters are frequently lost during input.
## Expected Behavior
The complete string "310110197211165036" should be entered accurately every time.
## Actual Behavior
Sometimes characters are lost during input, resulting in incomplete strings such as "310097215036".
## Root Cause Analysis
After investigating the source code in `packages/android/src/device.ts`, I found the issue occurs in the `keyboardType` method around lines 1453-1464:
```typescript
} else {
// inputText cannot handle newlines, so split by \n and press Enter between segments.
const segments = text.split('\n');
for (let i = 0; i < segments.length; i++) {
if (segments[i].length > 0) {
await adb.inputText(segments[i]); // Problem: No delay between inputs
}
if (i < segments.length - 1) {
await adb.keyevent(66);
}
}
}
```
The reasons for the issue are:
1. Pure numeric strings use the native `adb.inputText()` method.
2. This method inputs the entire string at once, with no per-character delay.
3. On slower devices or certain input fields, this can cause characters to be dropped due to processing speed limitations.
## Suggested Solutions
### Solution 1: Add Delay Between Inputs (Recommended)
Modify the keyboardType method to add a small delay after each inputText call:
```typescript
} else {
const segments = text.split('\n');
for (let i = 0; i < segments.length; i++) {
if (segments[i].length > 0) {
await adb.inputText(segments[i]);
await sleep(50); // Add delay to ensure proper processing
}
if (i < segments.length - 1) {
await adb.keyevent(66);
}
}
}
```
### Solution 2: Improve Default IME Strategy Handling
Currently, the default strategy (`yadb-for-non-ascii`) does not apply yadb for pure numeric strings. Consider making the default behavior more robust for all types of long strings:
```typescript
// Force yadb for long strings (>10 characters), regardless of content
const useYadb =
IME_STRATEGY === IME_STRATEGY_ALWAYS_YADB ||
(IME_STRATEGY === IME_STRATEGY_YADB_FOR_NON_ASCII &&
(this.shouldUseYadbForText(text) || text.length > 10)); // Apply to long strings
```
### Solution 3: Allow Configurable Delay
Add an optional parameter to allow users to configure the delay between characters:
```typescript
async keyboardType(
text: string,
options?: AndroidDeviceInputOpt & { charDelayMs?: number },
): Promise<void> {
// Implement functionality with configurable delay
}
```
## Temporary User Workaround
Users encountering this issue can explicitly set the IME strategy to always use yadb to bypass the problem:
```typescript
const device = new AndroidDevice(deviceId, {
imeStrategy: 'always-yadb' // Force use of more stable input method
});
```
## Impact
This bug affects the reliability of agent-driven numeric data input, especially in scenarios requiring authentication or long numeric strings.
---
Let me know if you need further adjustments or formatting for GitHub issues!
### Reproduce link
https://midscenejs.com/android-api-reference.html
### Reproduce Steps
{
"taskId": "2ca0a0f1-15c3-4e1e-90e8-22b4a1e97cdd",
"status": "finished",
"type": "Action Space",
"subType": "Input",
"param": {
"value": "310110197211165036"
},
"timing": {
"start": 1772457964468,
"getUiContextStart": 1772457964469,
"getUiContextEnd": 1772457964469,
"callActionStart": 1772457964671,
"callActionEnd": 1772457966810,
"captureAfterCallingSnapshotStart": 1772457967110,
"captureAfterCallingSnapshotEnd": 1772457967619,
"end": 1772457967619,
"cost": 3151
},
"uiContext": {
"shotSize": {
"width": 1264,
"height": 2800
},
"deprecatedDpr": 3.5013850415512464,
"screenshot": {
"base64": "data:image..."
},
"shrunkShotToLogicalRatio": 3.5013850415512464
},
"recorder": [
{
"type": "screenshot",
"ts": 1772457967619,
"screenshot": {
"base64": "data:image..."
},
"timing": "after-calling"
}
]
}
<img width="1008" height="1462" alt="Image" src="https://github.com/user-attachments/assets/bb220aab-3b5b-4976-abf0-d77522bcf594" />
关闭于 2026-03-09 0 条评论