browserPressKey
Simulate keyboard key presses on web pages using Playwright, with options to target specific elements and adjust wait times for efficient browser automation.
Instructions
按键盘按键
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| key | Yes | 按键名称,如 'Enter', 'Tab', 'ArrowDown' 等 | |
| pageId | Yes | 页面ID | |
| ref | No | 可选:元素的xp引用值,如果指定则在该元素上按键 | |
| waitForTimeout | No | 操作后等待获取快照的延迟时间(毫秒,默认2000) |
Implementation Reference
- lib/tools/keyboard.js:32-39 (handler)The handler function that presses the specified key using the page's keyboard.press method. This is the core implementation of the tool logic.handle: async (tab, params, response) => { response.setIncludeSnapshot(); response.addCode(`// Press ${params.key}`); response.addCode(`await page.keyboard.press('${params.key}');`); await tab.waitForCompletion(async () => { await tab.page.keyboard.press(params.key); }); },
- lib/tools/keyboard.js:23-31 (schema)Tool schema defining the name 'browser_press_key' (maps to client 'browserPressKey'), input schema requiring 'key' string, and metadata.schema: { name: 'browser_press_key', title: 'Press a key', description: 'Press a key on the keyboard', inputSchema: z.object({ key: z.string().describe('Name of the key to press or a character to generate, such as `ArrowLeft` or `a`'), }), type: 'destructive', },
- lib/tools.js:32-49 (registration)Central registration of all browser tools by spreading exports from individual modules, including keyboard.js which provides browser_press_key.export const allTools = [ ...common, ...console, ...dialogs, ...evaluate, ...files, ...form, ...install, ...keyboard, ...navigate, ...network, ...mouse, ...pdf, ...screenshot, ...snapshot, ...tabs, ...wait, ];
- lib/browserServerBackend.js:21-52 (registration)MCP backend registration: imports filteredTools, sets _tools, and exposes tool schemas via listTools() for the protocol.import { filteredTools } from './tools.js'; import { toMcpTool } from './mcp/tool.js'; export class BrowserServerBackend { _tools; _context; _sessionLog; _config; _browserContextFactory; constructor(config, factory) { this._config = config; this._browserContextFactory = factory; this._tools = filteredTools(config); } async initialize(server, clientVersion, roots) { let rootPath; if (roots.length > 0) { const firstRootUri = roots[0]?.uri; const url = firstRootUri ? new URL(firstRootUri) : undefined; rootPath = url ? fileURLToPath(url) : undefined; } this._sessionLog = this._config.saveSession ? await SessionLog.create(this._config, rootPath) : undefined; this._context = new Context({ tools: this._tools, config: this._config, browserContextFactory: this._browserContextFactory, sessionLog: this._sessionLog, clientInfo: { ...clientVersion, rootPath }, }); } async listTools() { return this._tools.map(tool => toMcpTool(tool.schema)); }