android_send_key_event
Simulate hardware button presses on Android devices, such as HOME, BACK, or ENTER, for automated testing and remote control.
Instructions
Send a key event to the Android device (e.g., KEYEVENT_HOME, KEYEVENT_BACK, KEYEVENT_ENTER)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| keyCode | Yes | Key event code (e.g., KEYEVENT_HOME, KEYEVENT_BACK, KEYEVENT_ENTER, 3 for HOME, 4 for BACK). Can be key name or numeric code. | |
| deviceSerial | No | Specific device serial number to target (optional) |
Implementation Reference
- src/handlers.ts:678-694 (handler)The main handler function for the 'android_send_key_event' tool. It extracts arguments, calls the ADB wrapper's sendKeyEvent method, and returns a success message or throws an error.export async function handleSendKeyEvent(adb: ADBWrapper, args: SendKeyEventArgs): Promise<{ content: Array<{ type: string; text: string }> }> { const { keyCode, deviceSerial } = args; try { await adb.sendKeyEvent(keyCode, deviceSerial); return { content: [ { type: 'text', text: `Key event sent: ${keyCode}`, }, ], }; } catch (error) { throw new Error(`Failed to send key event: ${error instanceof Error ? error.message : String(error)}`); } }
- src/index.ts:398-415 (schema)The input schema and tool metadata definition for 'android_send_key_event' used in tool listing and validation.{ name: 'android_send_key_event', description: 'Send a key event to the Android device (e.g., KEYEVENT_HOME, KEYEVENT_BACK, KEYEVENT_ENTER)', inputSchema: { type: 'object', properties: { keyCode: { type: 'string', description: 'Key event code (e.g., KEYEVENT_HOME, KEYEVENT_BACK, KEYEVENT_ENTER, 3 for HOME, 4 for BACK). Can be key name or numeric code.', }, deviceSerial: { type: 'string', description: 'Specific device serial number to target (optional)', }, }, required: ['keyCode'], }, },
- src/index.ts:502-503 (registration)The switch case registration that dispatches calls to the 'android_send_key_event' handler function.case 'android_send_key_event': return await handleSendKeyEvent(this.adb, args as any);
- src/adb-wrapper.ts:398-401 (helper)Supporting utility method in ADBWrapper class that executes the actual ADB 'shell input keyevent' command to send the key event to the device.async sendKeyEvent(keyCode: string, deviceSerial?: string): Promise<void> { const device = await this.getTargetDevice(deviceSerial); await this.exec(['shell', 'input', 'keyevent', keyCode], device); }
- src/handlers.ts:673-676 (schema)TypeScript interface defining the input arguments for the send key event handler.interface SendKeyEventArgs { keyCode: string; deviceSerial?: string; }