input_keyevent
Simulate Android device key presses like BACK, HOME, ENTER, or DELETE for automation testing and device control via ADB commands.
Instructions
Send key events (BACK, HOME, ENTER, DELETE)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| key | Yes | Key event to send |
Implementation Reference
- src/tools/handlers.ts:138-166 (handler)Executes ADB keyevent command using mapped keycodes for BACK, HOME, ENTER, DELETE; captures UI dump afterward and returns formatted response.input_keyevent: async (args: any) => { const { key } = args as { key: string }; const keyCodeMap = { BACK: '4', HOME: '3', ENTER: '66', DELETE: '67', }; const keyCode = keyCodeMap[key as keyof typeof keyCodeMap]; if (!keyCode) { throw new McpError(ErrorCode.InvalidParams, `Invalid key: ${key}`); } await executeCommand(`adb shell input keyevent ${keyCode}`); const uiContent = await captureUIContent(false); return { content: [ { type: 'text', text: `Key event sent: ${key} (${keyCode})`, }, ...uiContent, ], }; },
- src/tools/definitions.ts:70-84 (schema)Input schema defining the 'key' parameter with allowed values: BACK, HOME, ENTER, DELETE.{ name: 'input_keyevent', description: 'Send key events (BACK, HOME, ENTER, DELETE)', inputSchema: { type: 'object', properties: { key: { type: 'string', enum: ['BACK', 'HOME', 'ENTER', 'DELETE'], description: 'Key event to send', }, }, required: ['key'], }, },
- src/index.ts:26-30 (registration)Registers tool definitions (including input_keyevent schema) for the MCP ListTools request.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: toolDefinitions, }; });
- src/index.ts:32-46 (registration)Registers generic tool handler dispatcher that invokes input_keyevent handler from toolHandlers object based on tool name.server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; try { const handler = toolHandlers[name as keyof typeof toolHandlers]; if (!handler) { throw new McpError(ErrorCode.MethodNotFound, `Unknown tool: ${name}`); } return await handler(args); } catch (error) { const errorMessage = error instanceof Error ? error.message : String(error); throw new McpError(ErrorCode.InternalError, `Tool execution failed: ${errorMessage}`); } });