@blockly/keyboard-navigation

中文文档
3.0.5发布于 2026/04/08Apache-2.0TypeScriptESM + CJS零依赖近 30 天下载 1.2万

A plugin for keyboard navigation.

原始使用文档
文档目录9 节
  1. End-user instructions
  2. Giving feedback
  3. Using in your app
  4. Installation
  5. Usage
  6. Add shortcuts to page
  7. Use with custom Toolbox implementation
  8. Use with cross-tab-copy-paste plugin
  9. Contributing

@blockly/keyboard-navigation

This plugin for Blockly enables keyboard navigation. It includes different actions that might help visually impaired and motor impaired people navigate a Blockly workspace.

Keyboard navigation and screenreader support are closely coupled. The Blockly team intends to add screenreader support incrementally in Q3 of 2025, as we validate the general approach to navigation.

For more planning and timeline information please see our accessibility site.

End-user instructions

You can explore the current state of the plugin on the test page.

To use keyboard navigation, click on the workspace or press tab until you reach the workspace.

Once browser focus is on the Blockly workspace, you can use arrow keys to move a cursor around the workspace. You can use keyboard shortcuts to take actions at the cursor.

For instance, you can move the cursor to a dropdown field and press the Enter key to edit the field.

The available actions depend on the cursor location. For instance, if the cursor is on a block you can copy it with Ctrl + C.

You can open the toolbox by pressing T or pressing Tab until the toolbox is highlighted. Just like the workspace, you can use the arrow keys to move around the toolbox and select a block. Pressing Enter will place the block at the cursor's location on the workspace.

If you don't know which actions are available, you can press / to see a list of actions.

Giving feedback

If you use the test page and find a bug, please let us know by opening an issue on this repository! Include information about how to reproduce the bug, what the bad behaviour was, and what you expected it to do. The Blockly team will triage the bug and add it to the roadmap.

Using in your app

Installation

Using this plugin requires using at least Blockly v12.2.0. You can find the current minimum required version of Blockly in the peerDependencies section of the package.json file for the plugin.

Yarn

yarn add @blockly/keyboard-navigation

npm

npm install @blockly/keyboard-navigation --save

Usage

import * as Blockly from 'blockly';
import {KeyboardNavigation} from '@blockly/keyboard-navigation';

// Register styles. Only do this once per page-load.
// Must be done before calling Blockly.inject.
KeyboardNavigation.registerKeyboardNavigationStyles();

// Register the default toolbox. Only do this once per page-load.
// Must be done before calling Blockly.inject.
// See instructions below if you don't use the default toolbox.
KeyboardNavigation.registerNavigationDeferringToolbox();

// Inject Blockly.
const workspace = Blockly.inject('blocklyDiv', {
  toolbox: toolboxCategories,
});
// Initialize plugin.
const keyboardNav = new KeyboardNavigation(workspace);

Add shortcuts to page

In order to see the keyboard help popup when the user presses /, you need to add an empty div element to the hosting page that has the Blockly div element with the id "shortcuts". The plugin will take care of layout and formatting.

...
<div id="shortcuts"></div>
...
<div id="blockly"></div>
...

Use with custom Toolbox implementation

If you supply your own subclass of Toolbox, you need to override the onKeyDown_ method to make it a no-op. The base class has its own keyboard navigation built-in that you need to disable.

class YourCustomToolbox extends Blockly.Toolbox {
  protected override onKeyDown_(e: KeyboardEvent) {
    // No-op, prevent keyboard handling by superclass in order to defer to
    // global keyboard navigation.
  }
}

Use with cross-tab-copy-paste plugin

This plugin adds context menu items for copying & pasting. It also adds feedback to copying & pasting as toasts that are shown to the user upon successful copy or cut. It is compatible with the @blockly/plugin-cross-tab-copy-paste by following these steps:

import * as Blockly from 'blockly';
import {KeyboardNavigation} from '@blockly/keyboard-navigation';
import {CrossTabCopyPaste} from '@blockly/plugin-cross-tab-copy-paste';

// Register styles. Only do this once per page-load.
// Must be done before calling Blockly.inject.
KeyboardNavigation.registerKeyboardNavigationStyles();

// Inject Blockly.
const workspace = Blockly.inject('blocklyDiv', {
  toolbox: toolboxCategories,
});

// Initialize cross-tab-copy-paste
// Must be done before keyboard-navigation
const crossTabOptions = {
  // Don't use the context menu options from the ctcp plugin,
  // because the keyboard-navigation plugin provides its own.
  contextMenu: false,
  shortcut: true,
};
const plugin = new CrossTabCopyPaste();
plugin.init(crossTabOptions, () => {
  console.log('Use this error callback to handle TypeError while pasting');
});

// Initialize keyboard-navigation.
// You must pass the `allowCrossWorkspacePaste` option in order for paste
// to appear correctly enabled/disabled in the context menu.
const keyboardNav = new KeyboardNavigation(workspace, {
  allowCrossWorkspacePaste: true,
});

Contributing

To learn more about contributing to this project, see the contributing page.

版本与发布记录

@blockly/keyboard-navigation 共发布 103 个版本,首次发布于 2021/03/26;过去 12 个月发布了 1 个版本。

版本发布日期距今
3.0.5latest2026/04/087 个月前
3.0.32025/10/071.1 年前
3.0.22025/09/181.1 年前
4.0.0-beta.0beta2025/09/171.1 年前
3.0.12025/09/031.2 年前
3.0.02025/08/291.2 年前

关于 @blockly/keyboard-navigation 的常见问题

@blockly/keyboard-navigation 是什么?

A plugin for keyboard navigation.

如何安装 @blockly/keyboard-navigation?

在项目目录运行 npm install @blockly/keyboard-navigation,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:blockly(^12.3.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@blockly/keyboard-navigation 支持 TypeScript 吗?

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

@blockly/keyboard-navigation 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@blockly/keyboard-navigation 可以商用吗?

@blockly/keyboard-navigation 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@blockly/keyboard-navigation 的下载量有多少?

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

@blockly/keyboard-navigation 还在维护吗?

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