android_touch
Simulate touch interactions on Android device screens by specifying exact coordinates and duration for automated testing or remote control.
Instructions
Simulate a touch event at specific screen coordinates
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| x | Yes | X coordinate | |
| y | Yes | Y coordinate | |
| duration | No | Touch duration in milliseconds (default: 100) | |
| deviceSerial | No | Specific device serial number to target (optional) |
Implementation Reference
- src/handlers.ts:135-163 (handler)The main handler function for the 'android_touch' tool. Validates input parameters (x, y, duration) and delegates the touch execution to ADBWrapper.touch, then returns a success message.export async function touchHandler( adb: ADBWrapper, args: any ): Promise<{ content: Array<{ type: string; text: string }> }> { const { x, y, duration = 100, deviceSerial } = args as TouchArgs; if (typeof x !== 'number' || typeof y !== 'number') { throw new Error('Invalid coordinates: x and y must be numbers'); } if (x < 0 || y < 0) { throw new Error('Invalid coordinates: x and y must be positive'); } try { await adb.touch(x, y, duration, deviceSerial); return { content: [ { type: 'text', text: `Touch executed at (${x}, ${y}) with duration ${duration}ms`, }, ], }; } catch (error) { throw new Error(`Touch failed: ${error instanceof Error ? error.message : String(error)}`); } }
- src/index.ts:58-84 (schema)The input schema and description for the 'android_touch' tool, provided in the ListTools response.{ name: 'android_touch', description: 'Simulate a touch event at specific screen coordinates', inputSchema: { type: 'object', properties: { x: { type: 'number', description: 'X coordinate', }, y: { type: 'number', description: 'Y coordinate', }, duration: { type: 'number', description: 'Touch duration in milliseconds (default: 100)', default: 100, }, deviceSerial: { type: 'string', description: 'Specific device serial number to target (optional)', }, }, required: ['x', 'y'], }, },
- src/index.ts:466-467 (registration)Registration of the 'android_touch' tool handler in the switch statement for CallToolRequestSchema.case 'android_touch': return await touchHandler(this.adb, args);
- src/handlers.ts:8-13 (schema)TypeScript interface defining the expected input shape for the touchHandler.interface TouchArgs { x: number; y: number; duration?: number; deviceSerial?: string; }
- src/adb-wrapper.ts:293-311 (helper)Core implementation of the touch action in ADBWrapper.touch method, using 'adb shell input tap' for short touches or 'swipe' for longer durations.async touch( x: number, y: number, duration: number = 100, deviceSerial?: string ): Promise<void> { const device = await this.getTargetDevice(deviceSerial); if (duration <= 100) { // Simple tap await this.exec(['shell', 'input', 'tap', String(x), String(y)], device); } else { // Long press using swipe with same start/end coordinates await this.exec( ['shell', 'input', 'swipe', String(x), String(y), String(x), String(y), String(duration)], device ); } }