pressKey
Simulate keyboard key presses during browser automation to interact with web elements, submit forms, or navigate pages programmatically.
Instructions
Press a key on the keyboard
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| key | Yes | Key to press (e.g., 'Enter', 'Escape', 'ArrowDown', etc.) |
Implementation Reference
- src/controllers/playwright.ts:666-678 (handler)The core implementation of the pressKey tool handler in PlaywrightController, simulating key press using page.keyboard.press(key).async pressKey(key: string): Promise<void> { try { if (!this.isInitialized() || !this.state.page) { throw new Error('Browser not initialized'); } this.log('Pressing key', { key }); await this.state.page.keyboard.press(key); this.log('Key press complete'); } catch (error: any) { console.error('Press key error:', error); throw new BrowserError('Failed to press key', 'Check if the key name is valid'); } }
- src/server.ts:312-325 (schema)Tool schema definition for pressKey, specifying the input parameter 'key' as a required string.const PRESS_KEY_TOOL: Tool = { name: "pressKey", description: "Press a key on the keyboard", inputSchema: { type: "object", properties: { key: { type: "string", description: "Key to press (e.g., 'Enter', 'Escape', 'ArrowDown', etc.)" } }, required: ["key"] } };
- src/server.ts:539-539 (registration)Registers the pressKey tool in the tools dictionary passed to the MCP server's capabilities.pressKey: PRESS_KEY_TOOL,
- src/server.ts:834-845 (registration)MCP server request handler for callTool 'pressKey', dispatching to playwrightController.pressKey with input validation.case 'pressKey': { if (!args.key) { return { content: [{ type: "text", text: "Key is required" }], isError: true }; } await playwrightController.pressKey(args.key as string); return { content: [{ type: "text", text: "Key pressed successfully" }] }; }