定义键盘快捷键
Accelerators是多个字符串,包含了多个修饰符和键码,使用+字符连接,被用于定义你的应用程序的快捷键。
例子:
CommandOrControl+ACommandOrControl+Shift+Z
快捷键被globalShortcut模块使用register模块注册。
const {app, globalShortcut} = require('electron')
app.on('ready', () => {
// 注册一个‘CommandOrControl+Y’的快捷键监听。
globalShortcut.register('CommandOrControl+Y', () => {
// 当Y 和Command/Control被按下时要做的事情
})
})
不同平台的注意事项
Linux和Windows中,Command键没有任何作用,所以我们使用CommandOrControl来定义一些accelerators,它在macOS下可以代表Command,并且在Linux和Windows下代表Control to define some accelerators.
使用Alt代替Option。Option键只在macOS中存在,而Alt键在所有系统中都存在。
Super键映射到Windows中是Windows键,映射到macOS中是Cmd键。
可以使用的修饰符
-
Command(orCmdfor short) -
Control(orCtrlfor short) -
CommandOrControl(orCmdOrCtrlfor short) AltOptionAltGrShiftSuper
可用的键码
-
0to9 -
AtoZ -
F1toF24 - Punctuations like
~,!,@,#,$, etc. PlusSpaceTabBackspaceDeleteInsert-
Return(orEnteras alias) -
Up,Down,LeftandRight -
HomeandEnd -
PageUpandPageDown -
Escape(orEscfor short) -
VolumeUp,VolumeDownandVolumeMute -
MediaNextTrack,MediaPreviousTrack,MediaStopandMediaPlayPause PrintScreen