Keyboard
Previous
Clipboard
Next
History
Loading...
Enable keyboard shortcuts via the Keyboard plugin:
import { Graph, Keyboard } from '@antv/x6'const graph = new Graph({background: {color: '#F2F7FA',},})graph.use(new Keyboard({enabled: true,}),)
| Property Name | Type | Default Value | Required | Description |
|---|---|---|---|---|
| global | boolean | false | Whether 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, it must be focused 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 pressedreturn false}return true},}),)
bindKey(keys: string | string[],callback: (e: KeyboardEvent) => void,action?: 'keypress' | 'keydown' | 'keyup',): this
Bind keyboard shortcuts.
unbindKey(keys: string | string[],action?: 'keypress' | 'keydown' | 'keyup',): this
Unbind keyboard shortcuts.
clearKeys(): this
Clear all keyboard shortcuts.
triggerKey(keys: string,action?: 'keypress' | 'keydown' | 'keyup',): this
Manually trigger keyboard shortcuts.
isKeyboardEnabled(): boolean
Returns whether keyboard events are enabled.
enableKeyboard(): this
Enable keyboard events.
disableKeyboard(): this
Disable keyboard events.
toggleKeyboard(enabled?: boolean): this
Toggle the enabled state of keyboard events. The parameters are as follows:
| Name | Type | Required | Default Value | Description |
|---|---|---|---|---|
| enabled | boolean | - | Whether to enable keyboard events; if omitted, toggles the current enabled state. |
Ctrl + CCopy CellCtrl + VPaste Cell