跳到主要内容

快捷键

在本章节中主要介绍快捷键插件相关的知识,通过阅读,你可以了解到

  • 如何为画布绑定快捷键

使用

你可以通过插件 Keyboard 启用快捷键功能,示例:

import { Graph, Keyboard } from '@antv/x6'

const graph = new Graph({
  background: {
    color: '#F2F7FA',
  },
})
graph.use(
  new Keyboard({
    enabled: true,
  }),
)

演示

import './index.less'
import { createRoot } from 'react-dom/client'
import * as React from 'react'
import { Graph, Clipboard, Keyboard, Selection } from '@antv/x6'

export default class Example extends React.Component {
  private container: HTMLDivElement
  private graph: Graph

  componentDidMount() {
    this.graph = new Graph({
      container: this.container,
      background: {
        color: '#F2F7FA',
      },
    })

    this.graph.use(
      new Clipboard({
        enabled: true,
      }),
    )
    this.graph.use(
      new Selection({
        enabled: true,
        showNodeSelectionBox: true,
      }),
    )
    this.graph.use(
      new Keyboard({
        enabled: true,
        global: true,
      }),
    )

    this.graph.addNode({
      x: 280,
      y: 100,
      width: 100,
      height: 40,
      label: 'Rect',
      attrs: {
        body: {
          stroke: '#8f8f8f',
          strokeWidth: 1,
          fill: '#fff',
          rx: 6,
          ry: 6,
        },
      },
    })

    const source = this.graph.addNode({
      x: 32,
      y: 32,
      width: 100,
      height: 40,
      label: 'Hello',
      attrs: {
        body: {
          stroke: '#8f8f8f',
          strokeWidth: 1,
          fill: '#fff',
          rx: 6,
          ry: 6,
        },
      },
    })

    const target = this.graph.addNode({
      shape: 'circle',
      x: 160,
      y: 180,
      width: 60,
      height: 60,
      label: 'World',
      attrs: {
        body: {
          stroke: '#8f8f8f',
          strokeWidth: 1,
          fill: '#fff',
        },
      },
    })

    this.graph.addEdge({
      source,
      target,
      attrs: {
        line: {
          stroke: '#8f8f8f',
          strokeWidth: 1,
        },
      },
    })

    this.graph.bindKey('ctrl+c', () => {
      const cells = this.graph.getSelectedCells()
      if (cells.length) {
        this.graph.copy(cells)
      }
      return false
    })

    this.graph.bindKey('ctrl+v', () => {
      if (!this.graph.isClipboardEmpty()) {
        const cells = this.graph.paste({ offset: 32 })
        this.graph.cleanSelection()
        this.graph.select(cells)
      }
      return false
    })
  }

  refContainer = (container: HTMLDivElement) => {
    this.container = container
  }

  render() {
    return (
      <div className="keyboard-app">
        <div className="app-info">
          <p>
            <span>Ctrl + C</span>Copy Cell
            <span style={{ marginLeft: 24 }}>Ctrl + V</span>Paste Cell
          </p>
        </div>
        <div className="app-content" ref={this.refContainer} />
      </div>
    )
  }
}

const root = createRoot(document.getElementById('container')!)
root.render(<Example />)
window.addEventListener('pagehide', () => root.unmount(), { once: true })

配置

属性名 类型 默认值 必选 描述
global boolean false 是否为全局键盘事件,设置为 true 时键盘事件绑定在 document 上,否则绑定在画布容器上。当绑定在画布容器上时,需要容器获得焦点才能触发键盘事件
format (this: Graph, key: string) => string - 绑定或解绑键盘事件时,格式化按键字符串
guard (this: Graph, e: KeyboardEvent) => boolean - 判断一个键盘事件是否应该被处理,返回 false 时对应的键盘事件被忽略

format 和 guard 配置使用如下:

graph.use(
  new Keyboard({
    enabled: true,
    format(key) {
      return key.replace(/\s/g, '').replace('cmd', 'command')
    },
  }),
)
// 下面语句被格式化后等同于 graph.bindKey('command', (e) => { })
graph.bindKey('cmd', (e) => {})

graph.use(
  new Keyboard({
    enabled: true,
    guard(this: Graph, e: KeyboardEvent) {
      if (e.altKey) {
        // 当按下 alt 键时,忽略所有键盘事件
        return false
      }
      return true
    },
  }),
)

API

graph.bindKey(…)

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

绑定快捷键。

graph.unbindKey(…)

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

解绑快捷键。

graph.clearKeys()

clearKeys(): this

清除所有快捷键。

graph.triggerKey()

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

手动触发快捷键。

graph.isKeyboardEnabled()

isKeyboardEnabled(): boolean

获取是否启用了键盘事件。

graph.enableKeyboard()

enableKeyboard(): this

启用键盘事件。

graph.disableKeyboard()

disableKeyboard(): this

禁用键盘事件。

graph.toggleKeyboard(…)

toggleKeyboard(enabled?: boolean): this

切换键盘事件的启用状态。参数如下:

名称 类型 必选 默认值 描述
enabled boolean - 是否启用键盘事件,缺省时切换键盘事件的启用状态。
© Copyright 2026 Ant Group Co., Ltd..备案号:京ICP备15032932号-38
全站搜索
文档与示例输入关键词开始搜索

输入关键词,查找文档、API 与示例

↑↓选择↵打开Esc关闭