跳转到主要内容

加速器

定义键盘快捷键。

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

示例

  • 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 和 Linux 上的 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
  • UpDownLeftRight
  • HomeEnd
  • PageUpPageDown
  • Escape(或简称为 Esc
  • VolumeUpVolumeDownVolumeMute
  • MediaNextTrackMediaPreviousTrackMediaStopMediaPlayPause
  • PrintScreen
  • 数字小键盘按键
    • num0 - num9
    • numdec - 小数点键
    • numadd - 小键盘 +
    • numsub - 小键盘 -
    • nummult - 小键盘 *
    • numdiv - 小键盘 ÷