Skip to main content
Glama

press_key

Press hardware or software keys on Android devices to navigate interfaces, control volume, power, and access functions like home, back, menu, camera, and search.

Instructions

Press a hardware/software key on the Android device. Common keycodes: HOME, BACK, MENU, ENTER, VOLUME_UP, VOLUME_DOWN, POWER, DEL, TAB, SPACE, APP_SWITCH, SEARCH, CAMERA.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
keycodeYesAndroid keycode name (e.g. HOME, BACK, ENTER, VOLUME_UP) or numeric code
device_idNoDevice serial number

Implementation Reference

  • The core handler function that executes the ADB keyevent command.
    export async function pressKey(keycode: string, deviceId?: string): Promise<string> {
      const resolved = await deviceManager.resolveDeviceId(deviceId);
      deviceManager.checkRateLimit(resolved);
    
      const validatedKeycode = validateKeycode(keycode);
      await adbShell(['input', 'keyevent', validatedKeycode], resolved);
    
      deviceManager.touchSession(resolved);
      log.info('Key press performed', { keycode: validatedKeycode, deviceId: resolved });
      return validatedKeycode;
    }
  • MCP tool registration and wrapper for the 'press_key' tool.
      'press_key',
      {
        description: 'Press a hardware/software key on the Android device. Common keycodes: HOME, BACK, MENU, ENTER, VOLUME_UP, VOLUME_DOWN, POWER, DEL, TAB, SPACE, APP_SWITCH, SEARCH, CAMERA.',
        inputSchema: {
          keycode: z.string().describe('Android keycode name (e.g. HOME, BACK, ENTER, VOLUME_UP) or numeric code'),
          device_id: z.string().optional().describe('Device serial number'),
        },
      },
      async ({ keycode, device_id }) => {
        return await metrics.measure('press_key', device_id || 'default', async () => {
          const resolved = await deviceManager.resolveDeviceId(device_id);
          const execCtx = executionEngine.preExecutionCheck('press_key', { keycode }, resolved);
    
          if (!execCtx.allowed) {
            return {
              content: [{
                type: 'text' as const,
                text: JSON.stringify({ success: false, blocked: true, reason: execCtx.blockReason, _context: execCtx.recentContext }, null, 2),
              }],
            };
          }
    
          const preHash = await capturePreActionState(resolved);
          const result = await pressKey(keycode, device_id);
          invalidateCaches(resolved);
          const verification = await verifyAction('press_key', resolved, preHash);
    
          return buildVerifiedResponse({ pressed_key: result }, execCtx, verification);
        });
      }
    );

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/divineDev-dotcom/android_mcp'

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