playwright_press_key
Simulate key presses on web pages to interact with elements, test functionality, or automate tasks. Optionally focus on specific elements using CSS selectors for precise control.
Instructions
Press a keyboard key
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| key | Yes | Key to press (e.g. 'Enter', 'ArrowDown', 'a') | |
| selector | No | Optional CSS selector to focus before pressing key |
Input Schema (JSON Schema)
{
"properties": {
"key": {
"description": "Key to press (e.g. 'Enter', 'ArrowDown', 'a')",
"type": "string"
},
"selector": {
"description": "Optional CSS selector to focus before pressing key",
"type": "string"
}
},
"required": [
"key"
],
"type": "object"
}
Implementation Reference
- src/tools/browser/interaction.ts:154-169 (handler)The PressKeyTool class provides the core implementation for the 'playwright_press_key' tool. Its execute method optionally focuses on a selector and presses the specified key using Playwright's page.keyboard.press.export class PressKeyTool extends BrowserToolBase { /** * Execute the key press tool */ async execute(args: any, context: ToolContext): Promise<ToolResponse> { return this.safeExecute(context, async (page) => { if (args.selector) { await page.waitForSelector(args.selector); await page.focus(args.selector); } await page.keyboard.press(args.key); return createSuccessResponse(`Pressed key: ${args.key}`); }); } }
- src/tools.ts:366-376 (schema)Defines the tool name, description, and input schema (parameters: key (required), selector (optional)) for validation and documentation.name: "playwright_press_key", description: "Press a keyboard key", inputSchema: { type: "object", properties: { key: { type: "string", description: "Key to press (e.g. 'Enter', 'ArrowDown', 'a')" }, selector: { type: "string", description: "Optional CSS selector to focus before pressing key" } }, required: ["key"], }, },
- src/toolHandler.ts:524-525 (registration)The switch case in handleToolCall dispatches calls to the 'playwright_press_key' tool to the PressKeyTool instance's execute method.case "playwright_press_key": return await pressKeyTool.execute(args, context);
- src/tools.ts:423-423 (registration)Includes 'playwright_press_key' in the BROWSER_TOOLS array, used to identify browser-requiring tools and conditionally launch browser."playwright_press_key",
- src/toolHandler.ts:314-314 (registration)Instantiates the PressKeyTool instance during tool initialization in toolHandler.ts.if (!pressKeyTool) pressKeyTool = new PressKeyTool(server);