跳至主要內容

加速器

定義鍵盤快捷鍵。

加速器是字串,可以包含多個修飾符和單個按鍵碼,並以 + 字元組合,用於定義整個應用程式的鍵盤快捷鍵。加速器不區分大小寫。

範例

  • 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 - 數字鍵盤 ÷