@a11y-oracle/keyboard-engine

中文文档
1.3.2发布于 2026/03/12MITTypeScriptES Module近 30 天下载 7851

Native CDP keyboard dispatch with modifier key support and focused element introspection

原始使用文档
文档目录8 节
  1. Installation
  2. Usage
  3. API Reference
  4. KeyboardEngine
  5. Supported Keys
  6. Types
  7. Exports
  8. How It Works

@a11y-oracle/keyboard-engine

CDP-based keyboard dispatch engine for accessibility testing. Sends native hardware-level keystrokes via Input.dispatchKeyEvent and reads focused element information via Runtime.evaluate.

This is a low-level building block used internally by @a11y-oracle/core-engine. Most users should use the Playwright or Cypress plugin instead.

Installation

npm install @a11y-oracle/keyboard-engine

Usage

import { KeyboardEngine } from '@a11y-oracle/keyboard-engine';

const keyboard = new KeyboardEngine(cdpSession);

// Press a key
await keyboard.press('Tab');

// Press with modifiers
await keyboard.press('Tab', { shift: true });   // Shift+Tab (backward)
await keyboard.press('a', { ctrl: true });       // Ctrl+A (select all)

// Get info about the currently focused element
const el = await keyboard.getFocusedElement();
if (el) {
  console.log(el.tag);      // "BUTTON"
  console.log(el.id);       // "submit-btn"
  console.log(el.role);     // "button"
  console.log(el.tabIndex); // 0
  console.log(el.rect);     // { x: 100, y: 200, width: 120, height: 40 }
}

API Reference

KeyboardEngine

constructor(cdp: CDPSessionLike)

Create a new keyboard engine.

  • cdp — Any object with a send(method, params?) method compatible with CDP.

press(key: string, modifiers?: ModifierKeys): Promise<void>

Dispatch a native key press via CDP Input.dispatchKeyEvent. Sends a keyDown event followed by a keyUp event, matching the behavior of a physical key press.

  • key — Key name from KEY_DEFINITIONS (see table below).
  • modifiers — Optional modifier keys to hold during the press.

Modifier keys map to the CDP bitmask: Alt = 1, Ctrl = 2, Meta = 4, Shift = 8.

Throws if the key name is not in KEY_DEFINITIONS.

getFocusedElement(): Promise<FocusedElementInfo | null>

Get information about the currently focused DOM element via Runtime.evaluate.

Returns null if no interactive element has focus (e.g., document.body or document.documentElement is focused).

Supported Keys

Key NameDescription
TabTab navigation
EnterActivate / submit
Space or ' 'Activate / toggle
EscapeCancel / close
ArrowUpNavigate up
ArrowDownNavigate down
ArrowLeftNavigate left
ArrowRightNavigate right
HomeJump to start
EndJump to end
BackspaceDelete backward
DeleteDelete forward

Types

KeyDefinition

CDP keyboard event parameters for a single key:

interface KeyDefinition {
  key: string;     // CDP key property (e.g. "Tab")
  code: string;    // CDP code property (e.g. "Tab")
  keyCode: number; // Windows virtual key code (e.g. 9)
}

ModifierKeys

Modifier keys for key press combinations:

interface ModifierKeys {
  shift?: boolean;
  ctrl?: boolean;
  alt?: boolean;
  meta?: boolean;
}

FocusedElementInfo

Information about the currently focused DOM element:

interface FocusedElementInfo {
  tag: string;        // "BUTTON"
  id: string;         // "submit-btn"
  className: string;  // "btn primary"
  textContent: string; // "Submit"
  role: string;       // "button" (from role attribute)
  ariaLabel: string;  // "Submit form" (from aria-label)
  tabIndex: number;   // 0
  rect: { x: number; y: number; width: number; height: number };
}

Exports

export { KeyboardEngine } from '@a11y-oracle/keyboard-engine';
export { KEY_DEFINITIONS } from '@a11y-oracle/keyboard-engine';
export type {
  KeyDefinition,
  ModifierKeys,
  FocusedElementInfo,
} from '@a11y-oracle/keyboard-engine';

How It Works

The engine uses two CDP domains:

  1. Input.dispatchKeyEvent — Sends real hardware-level keystroke events (keyDown + keyUp) directly to the browser. This bypasses synthetic JavaScript event dispatch (element.dispatchEvent()), so the browser handles focus management natively — just as if a user physically pressed the key.

  2. Runtime.evaluate — Executes a JavaScript expression in the page context to read document.activeElement properties (tag, id, class, role, aria-label, tabIndex, bounding rect).

版本与发布记录

@a11y-oracle/keyboard-engine 共发布 10 个版本,首次发布于 2026/03/05;过去 12 个月发布了 10 个版本。

版本发布日期距今
1.3.2latest2026/03/127 个月前
1.3.12026/03/117 个月前
1.3.02026/03/068 个月前
1.2.02026/03/068 个月前
1.1.42026/03/068 个月前
1.1.32026/03/068 个月前

关于 @a11y-oracle/keyboard-engine 的常见问题

@a11y-oracle/keyboard-engine 是什么?

Native CDP keyboard dispatch with modifier key support and focused element introspection

如何安装 @a11y-oracle/keyboard-engine?

在项目目录运行 npm install @a11y-oracle/keyboard-engine,或使用 pnpm add / yarn add / bun add。

@a11y-oracle/keyboard-engine 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@a11y-oracle/keyboard-engine 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@a11y-oracle/keyboard-engine 可以商用吗?

@a11y-oracle/keyboard-engine 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@a11y-oracle/keyboard-engine 的下载量有多少?

根据 npm 官方下载统计,@a11y-oracle/keyboard-engine 在 2026/10/05 – 2026/11/03 这 30 天内被下载 7,851 次,日均约 262 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@a11y-oracle/keyboard-engine 还在维护吗?

最新版本 1.3.2 发布于 2026/03/12(7 个月前);至今共发布 10 个版本,其中过去 12 个月发布了 10 个;首次发布于 2026/03/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。