logo

X6

  • Tutorials
  • API
  • Examples
  • Q&A
  • Change Log
  • XFlow
  • Productsantv logo arrow
  • 2.x
  • Introduction
  • Quickstart
  • Basic
    • Graph
    • Nodes
    • Edges
    • Connection Pile
    • Interaction
    • Events
    • Data Serialization
  • Intermediate
    • Connection Points
    • Tools
    • Group
    • React Nodes
    • Vue Nodes
    • Angular Nodes
    • HTML Nodes
  • Plugin
    • Graphic Transformations
    • Snapline
    • Clipboard
    • Keyboard
    • History
    • Selection Box
    • Scroller
    • Dnd
    • Mini Map
    • Stencil
    • Export
  • Upgrade to Version 2.x
  • Developer Tools

Keyboard

Previous
Clipboard
Next
History

Resource

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-Interactive solution
xtechLiven Experience technology
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

This section mainly introduces knowledge related to keyboard shortcuts. By reading, you can learn about

  • How to bind keyboard shortcuts to the canvas

Usage

We provide a standalone plugin package @antv/x6-plugin-keyboard to use keyboard shortcut functionality.

# npm
$ npm install @antv/x6-plugin-keyboard --save
# yarn
$ yarn add @antv/x6-plugin-keyboard

Then we use it in the code like this:

import { Keyboard } from '@antv/x6-plugin-keyboard'
const graph = new Graph({
background: {
color: '#F2F7FA',
},
})
graph.use(
new Keyboard({
enabled: true,
}),
)

Demo

Configuration

Property NameTypeDefault ValueRequiredDescription
globalbooleanfalseWhether to use global keyboard events. When set to true, keyboard events are bound to document; otherwise, they are bound to the canvas container. When bound to the canvas container, the container must gain focus to trigger keyboard events.
format(this:Graph, key: string) => string-Format the key string when binding or unbinding keyboard events.
guard(this:Graph,e:KeyboardEvent) => boolean-Determine whether a keyboard event should be processed. If it returns false, the corresponding keyboard event is ignored.

The format and guard configurations are used as follows:

graph.use(
new Keyboard({
enabled: true,
format(key) {
return key.replace(/\s/g, '').replace('cmd', 'command')
},
}),
)
// The statement below is equivalent to graph.bindKey('command', (e) => { })
graph.bindKey('cmd', (e) => {})
graph.use(
new Keyboard({
enabled: true,
guard(this: Graph, e: KeyboardEvent) {
if (e.altKey) {
// Ignore all keyboard events when the alt key is pressed
return false
}
return true
},
}),
)

API

graph.bindKey(...)

bindKey(
keys: string | string[],
callback: (e: KeyboardEvent) => void,
action?: 'keypress' | 'keydown' | 'keyup',
): this

Bind keyboard shortcuts.

graph.unbindKey(...)

unbindKey(
keys: string | string[],
action?: 'keypress' | 'keydown' | 'keyup',
): this

Unbind keyboard shortcuts.

graph.clearKeys()

clearKeys(): this

Clear all keyboard shortcuts.

graph.triggerKey()

triggerKey(
keys: string,
action?: 'keypress' | 'keydown' | 'keyup',
): this

Manually trigger keyboard shortcuts.

graph.isKeyboardEnabled()

isKeyboardEnabled(): boolean

Get whether keyboard events are enabled.

graph.enableKeyboard()

enableKeyboard(): this

Enable keyboard events.

graph.disableKeyboard()

disableKeyboard(): this

Disable keyboard events.

graph.toggleKeyboard(...)

toggleKeyboard(enabled?: boolean): this

Toggle the enabled state of keyboard events. The parameters are as follows:

NameTypeRequiredDefault ValueDescription
enabledboolean-Whether to enable keyboard events. If omitted, it toggles the enabled state of keyboard events.

Ctrl + CCopy CellCtrl + VPaste Cell