The Keyboard class provides methods to simulate keyboard input such as typing text and pressing keys. Access the Keyboard instance through page.keyboard.
Methods
type
Types text character by character.
await page.keyboard.type('Hello World');
await page.keyboard.type('text', { delay: 100 });
Time to wait between key presses in milliseconds. Defaults to 0.
press
Presses a single key or a key combination.
await page.keyboard.press('Enter');
await page.keyboard.press('Control+A');
await page.keyboard.press('Shift+ArrowLeft');
The key or key combination to press. See key values for valid values.
Time to wait between keydown and keyup in milliseconds. Defaults to 0.
down
Holds down a key.
await page.keyboard.down('Shift');
await page.keyboard.press('A');
await page.keyboard.up('Shift');
The key to hold down. See key values for valid values.
Releases a key.
await page.keyboard.down('Shift');
await page.keyboard.press('A');
await page.keyboard.up('Shift');
The key to release. See key values for valid values.
insertText
Inserts text without simulating key presses.
await page.keyboard.insertText('Hello World');
This method dispatches an input event directly, which is much faster than type() but does not trigger keydown, keypress, or keyup events.
Example Usage
Type Text with Delay
await page.keyboard.type('Hello World', { delay: 100 });
Keyboard Shortcuts
// Select all text
await page.keyboard.press('Control+A');
// Copy text
await page.keyboard.press('Control+C');
// Paste text
await page.keyboard.press('Control+V');
Hold Modifier Keys
await page.keyboard.down('Shift');
await page.keyboard.press('KeyA');
await page.keyboard.press('KeyB');
await page.keyboard.press('KeyC');
await page.keyboard.up('Shift');
Press Special Keys
await page.keyboard.press('Enter');
await page.keyboard.press('Tab');
await page.keyboard.press('Escape');
await page.keyboard.press('ArrowDown');