跳到主要内容

加速器

定义键盘快捷键。

加速器是字符串,可以包含多个修饰符和单个键代码,用 + 字符组合,并用于定义应用程序中的键盘快捷键。加速器不区分大小写。

例子

  • CommandOrControl+A
  • CommandOrControl+Shift+Z

快捷方式使用 globalShortcut 模块的 register 方法注册,例如:

const { app, globalShortcut } = require('electron')

app.whenReady().then(() => {
// Register a 'CommandOrControl+Y' shortcut listener.
globalShortcut.register('CommandOrControl+Y', () => {
// Do stuff when Y and either Command/Control is pressed.
})
})

平台提示

在 Linux 和 Windows 上,Command 键没有任何效果,因此使用 CommandOrControl,它代表 macOS 上的 Command,Linux 和 Windows 上的 Control 来定义一些加速器。

使用 Alt 代替 OptionOption 键仅存在于 macOS 上,而 Alt 键在所有平台上都可用。

Super(或 Meta)键映射到 Windows 上的 Windows 键和 macOS 上的 Cmd

可用修饰符

  • Command (或简写为 Cmd)
  • Control (或简写为 Ctrl)
  • CommandOrControl (或简写为 CmdOrCtrl)
  • Alt
  • Option
  • AltGr
  • Shift
  • Super
  • Meta

可用键代码

  • 09
  • AZ
  • F1F24
  • 各种标点符号: ), !, @, #, $, %, ^, &, *, (, :, ;, :, +, =, <, ,, _, -, >, ., ?, /, ~, `, {, ], [, |, \, }, "
  • Plus
  • Space
  • Tab
  • Capslock
  • Numlock
  • Scrolllock
  • Backspace
  • Delete
  • Insert
  • Return (或别名 Enter)
  • Up, Down, LeftRight
  • HomeEnd
  • PageUpPageDown
  • Escape (或简写为 Esc)
  • VolumeUp, VolumeDownVolumeMute
  • MediaNextTrack, MediaPreviousTrack, MediaStopMediaPlayPause
  • PrintScreen
  • 数字键盘按键
    • num0 - num9
    • numdec - 小数键
    • numadd - 数字键盘 +
    • numsub - 数字键盘 -
    • nummult - 数字键盘 *
    • numdiv - 数字键盘 ÷