Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Puppeteer Puppeteer Cdp Input

From Leeroopedia
Property Value
sources packages/puppeteer-core/src/cdp/Input.ts
domains Input Simulation, Keyboard, Mouse, Touch, CDP
last_updated 2026-02-12 00:00 GMT

Overview

Description

The Input module provides CDP-specific implementations for simulating keyboard, mouse, and touch input. It contains four main classes:

CdpKeyboard -- Simulates keyboard input via the Input.dispatchKeyEvent CDP command. Key features:

  • Tracks pressed keys and modifier state (Alt, Control, Meta, Shift) as a bitmask.
  • Converts key names to full key descriptions (keyCode, key, text, code, location) using the US keyboard layout definitions.
  • Supports down(), up(), press() (down + up with optional delay), type() (types text character by character), and sendCharacter() (inserts text directly).
  • Handles auto-repeat for held keys and shift-modified key mappings.

CdpMouse -- Simulates mouse input via Input.dispatchMouseEvent and Input.dispatchDragEvent. Key features:

  • Maintains transactional mouse state (position and pressed buttons) that supports concurrent operations.
  • Supports move() with interpolation steps, down(), up(), click() (with configurable count and delay), wheel(), and reset().
  • Supports all five mouse buttons: Left, Right, Middle, Back, Forward (mapped to protocol button flags).
  • Supports drag-and-drop operations via drag(), dragEnter(), dragOver(), drop(), and dragAndDrop().
  • Uses a transaction system with commit/rollback for state consistency during async operations.

CdpTouchscreen -- Simulates multi-touch input via Input.dispatchTouchEvent. Creates CdpTouchHandle instances that represent individual touch points with IDs, coordinates, radius, and force.

CdpTouchHandle -- Represents a single active touch point. Supports start(), move(), and end() operations.

Usage

Input classes are accessed via page.keyboard, page.mouse, and page.touchscreen. They are created internally by CdpPage.

Code Reference

Source Location: packages/puppeteer-core/src/cdp/Input.ts (653 lines)

Signature:

export class CdpKeyboard extends Keyboard {
  constructor(client: CDPSession);
  override async down(key: KeyInput, options?: Readonly<KeyDownOptions>): Promise<void>;
  override async up(key: KeyInput): Promise<void>;
  override async sendCharacter(char: string): Promise<void>;
  override async type(text: string, options?: Readonly<KeyboardTypeOptions>): Promise<void>;
  override async press(key: KeyInput, options?: Readonly<KeyPressOptions>): Promise<void>;
}

export class CdpMouse extends Mouse {
  constructor(client: CDPSession, keyboard: CdpKeyboard);
  override async move(x: number, y: number, options?: Readonly<MouseMoveOptions>): 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<MouseClickOptions>): Promise<void>;
  override async wheel(options?: Readonly<MouseWheelOptions>): Promise<void>;
  override async drag(start: Point, target: Point): Promise<Protocol.Input.DragData>;
  override async dragAndDrop(start: Point, target: Point, options?: { delay?: number }): Promise<void>;
  override async reset(): Promise<void>;
}

export class CdpTouchscreen extends Touchscreen {
  constructor(client: CDPSession, keyboard: CdpKeyboard);
  override async touchStart(x: number, y: number): Promise<TouchHandle>;
}

export class CdpTouchHandle implements TouchHandle {
  async start(): Promise<void>;
  move(x: number, y: number): Promise<void>;
  async end(): Promise<void>;
}

Import:

import { CdpKeyboard, CdpMouse, CdpTouchscreen } from 'puppeteer-core/lib/cdp/Input.js';

I/O Contract

Inputs:

Parameter Type Required Description
key KeyInput Yes A key name from the US keyboard layout (e.g., 'Enter', 'a', 'ArrowUp')
x, y number Yes Screen coordinates for mouse/touch operations
options.delay number No Delay in ms between key down and key up, or between click down and up
options.button MouseButton No Mouse button (default: 'left')
options.clickCount number No Number of click events (default: 1)
options.steps number No Number of intermediate mouse move events (default: 1)
options.deltaX, options.deltaY number No Scroll amounts for mouse wheel
options.text string No Override text for key down event
options.commands string[] No Editing commands to send with key event

Outputs:

Output Type Description
drag() return Promise<Protocol.Input.DragData> Drag data from an intercepted drag operation
touchStart() return Promise<TouchHandle> A handle to control the touch point

Usage Examples

// Type text into an input
await page.click('#input');
await page.keyboard.type('Hello, World!', { delay: 100 });

// Press keyboard shortcuts
await page.keyboard.down('Control');
await page.keyboard.press('a');
await page.keyboard.up('Control');

// Click at coordinates
await page.mouse.click(100, 200);

// Double click
await page.mouse.click(100, 200, { count: 2 });

// Right click
await page.mouse.click(100, 200, { button: 'right' });

// Drag and drop
await page.mouse.dragAndDrop(
  { x: 100, y: 100 },
  { x: 300, y: 300 },
  { delay: 500 },
);

// Scroll with mouse wheel
await page.mouse.wheel({ deltaY: 100 });

// Move mouse with intermediate steps
await page.mouse.move(500, 500, { steps: 10 });

// Touch input
const touch = await page.touchscreen.touchStart(100, 200);
await touch.move(200, 300);
await touch.end();

// Tap (touchStart + touchEnd)
await page.touchscreen.touchStart(100, 200).then(t => t.end());

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment