Skip to main content
Glama

adb_press_key

Press a key on an Android device using key codes like BACK, HOME, or MENU to automate device interactions through the ADB MCP Server.

Instructions

Press a key on the device

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
keyCodeYesKey code to press (e.g., "BACK", "HOME", "MENU", or numeric code)
deviceIdNoDevice ID (optional)

Implementation Reference

  • The pressKey method executes the ADB shell input keyevent command to simulate pressing the specified keycode on the Android device, with error handling and device connection checks.
    async pressKey(keyCode: string | number, deviceId?: string) {
      try {
        const connected = await this.adbClient.isDeviceConnected(deviceId);
        if (!connected) {
          return {
            success: false,
            error: 'Device not connected',
            message: 'Cannot press key - device is not connected'
          };
        }
    
        const command = `shell input keyevent ${keyCode}`;
        const result = await this.adbClient.executeCommand(command, deviceId);
        
        if (!result.success) {
          return {
            success: false,
            error: result.error,
            message: 'Failed to press key'
          };
        }
    
        return {
          success: true,
          data: { 
            keyCode,
            deviceId: deviceId || this.adbClient.getDefaultDevice()
          },
          message: `Pressed key: ${keyCode}`
        };
      } catch (error: any) {
        return {
          success: false,
          error: error.message,
          message: 'Failed to press key'
        };
      }
    }
  • Defines the tool name, description, and input schema (keyCode required as string or number, optional deviceId) for adb_press_key in the ListTools response.
    name: 'adb_press_key',
    description: 'Press a key on the device',
    inputSchema: {
      type: 'object',
      properties: {
        keyCode: {
          type: ['string', 'number'],
          description: 'Key code to press (e.g., "BACK", "HOME", "MENU", or numeric code)',
        },
        deviceId: {
          type: 'string',
          description: 'Device ID (optional)',
        },
      },
      required: ['keyCode'],
    },
  • src/index.ts:449-450 (registration)
    Registers the tool handler by dispatching adb_press_key calls to ScreenTools.pressKey method in the CallToolRequest switch statement.
    case 'adb_press_key':
      return await this.handleToolCall(this.screenTools.pressKey(args?.keyCode as string | number, args?.deviceId as string));

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/richard0913/adb-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server