Implementation:Puppeteer Puppeteer Bidi Input
| Property | Value |
|---|---|
| Implementation | BidiKeyboard, BidiMouse, BidiTouchscreen |
| Source File | packages/puppeteer-core/src/bidi/Input.ts
|
| Repository | puppeteer/puppeteer |
| Lines | 742 |
| License | Apache-2.0 |
| Copyright | 2017 Google Inc. |
Overview
Description
This module provides the WebDriver BiDi implementations of Puppeteer's input device classes: BidiKeyboard, BidiMouse, and BidiTouchscreen. All three translate Puppeteer's high-level input API into BiDi input.performActions commands dispatched through the page's main frame browsing context.
BidiKeyboard handles:
down(key)/up(key)-- Individual key press and release viakeyDownandkeyUpactions.press(key, options)-- A combined key down and key up with an optional delay pause in between.type(text, options)-- Types a string character by character, using code point decomposition (not UTF-16 code units). Supports an optional delay between keystrokes.sendCharacter(char)-- Inserts a single character viadocument.execCommand('insertText')in the focused frame's isolated realm.
A comprehensive getBidiKeyValue() mapping function converts Puppeteer KeyInput names (e.g., 'Enter', 'ArrowLeft', 'F1', 'KeyA') to their corresponding WebDriver BiDi key values (Unicode Private Use Area characters for special keys).
BidiMouse handles:
move(x, y, options)-- Moves the mouse with optional intermediate steps for smooth movement.down(options)/up(options)-- Press and release mouse buttons.click(x, y, options)-- Combined move, press, and release with support for multi-click (double-click, triple-click) and delay.wheel(options)-- Scrolls using the wheel action type withdeltaXanddeltaY.- Drag operations (
drag,dragOver,dragEnter,drop,dragAndDrop) throwUnsupportedOperationas BiDi does not support drag actions.
BidiTouchscreen and its companion BidiTouchHandle handle:
touchStart(x, y, options)-- Creates a new touch point and performs a pointer down action with touch pointer type.BidiTouchHandle.move(x, y)-- Moves an active touch point.BidiTouchHandle.end()-- Lifts a touch point (pointer up).- Each touch gets a unique ID based on the
InputId.Fingerprefix and an incrementing counter.
All input actions are dispatched to the BiDi session using typed source action arrays with specific input source IDs: __puppeteer_mouse, __puppeteer_keyboard, __puppeteer_wheel, and __puppeteer_finger_N.
Usage
These classes are instantiated internally by BidiPage and exposed as page.keyboard, page.mouse, and page.touchscreen. Users interact with them through the standard Puppeteer input API.
Code Reference
Source Location
packages/puppeteer-core/src/bidi/Input.ts (GitHub)
Signature
export class BidiKeyboard extends Keyboard {
constructor(page: BidiPage);
override async down(key: KeyInput, _options?: Readonly<KeyDownOptions>): Promise<void>;
override async up(key: KeyInput): Promise<void>;
override async press(key: KeyInput, options?: Readonly<KeyPressOptions>): Promise<void>;
override async type(text: string, options?: Readonly<KeyboardTypeOptions>): Promise<void>;
override async sendCharacter(char: string): Promise<void>;
}
export class BidiMouse extends Mouse {
constructor(page: BidiPage);
override async reset(): Promise<void>;
override async move(x: number, y: number, options?: Readonly<BidiMouseMoveOptions>): Promise<void>;
override async down(options?: Readonly<MouseOptions>): Promise<void>;
override async up(options?: Readonly<MouseOptions>): Promise<void>;
override async click(x: number, y: number, options?: Readonly<BidiMouseClickOptions>): Promise<void>;
override async wheel(options?: Readonly<MouseWheelOptions>): Promise<void>;
}
export class BidiTouchscreen extends Touchscreen {
constructor(page: BidiPage);
override async touchStart(x: number, y: number, options?: BidiTouchMoveOptions): Promise<TouchHandle>;
}
Import
import {BidiKeyboard, BidiMouse, BidiTouchscreen} from './Input.js';
I/O Contract
Keyboard Inputs
| Method | Parameter | Type | Description |
|---|---|---|---|
| down / up | key | KeyInput |
Key name (e.g., 'Enter', 'ArrowLeft', 'KeyA', 'Shift') |
| press | key | KeyInput |
Key name to press and release |
| press | options.delay | number |
Delay in ms between keyDown and keyUp (default: 0) |
| type | text | string |
Text string to type character by character |
| type | options.delay | number |
Delay in ms between each keystroke (default: 0) |
| sendCharacter | char | string |
A single character to insert via execCommand |
Mouse Inputs
| Method | Parameter | Type | Description |
|---|---|---|---|
| move | x, y | number |
Target coordinates |
| move | options.steps | number |
Intermediate steps for smooth movement |
| click | x, y | number |
Click coordinates |
| click | options.button | MouseButton |
Button to click (Left, Middle, Right, Back, Forward) |
| click | options.count | number |
Number of clicks (default: 1) |
| click | options.delay | number |
Delay between mousedown and mouseup |
| wheel | options.deltaX | number |
Horizontal scroll amount |
| wheel | options.deltaY | number |
Vertical scroll amount |
Outputs
| Method | Return Type | Description |
|---|---|---|
| All keyboard/mouse methods | Promise<void> |
Resolves when the action is performed |
| touchStart | Promise<TouchHandle> |
Handle for controlling the touch point (move, end) |
Usage Examples
// Type text with keyboard
await page.keyboard.type('Hello, World!', {delay: 100});
// Press a key combination
await page.keyboard.down('Shift');
await page.keyboard.press('ArrowLeft');
await page.keyboard.up('Shift');
// Click at coordinates
await page.mouse.click(100, 200, {button: 'left'});
// Double-click
await page.mouse.click(100, 200, {count: 2});
// Move and scroll
await page.mouse.move(500, 300, {steps: 10});
await page.mouse.wheel({deltaY: 200});
// Touch interactions
const touch = await page.touchscreen.touchStart(100, 200);
await touch.move(150, 250);
await touch.end();