electron globalShortcut 快捷键与系统全局快捷键冲突


字数:360 阅读时长:1分钟 阅读:85

electron 开发自己的接口测试工具(Post Tools),在设置了 globalShortcut 快捷键后,发现应用中的快捷键与系统全局快捷键冲突了,导致系统快捷键不可正常使用。

electron globalShortcut

快捷键配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
export function initGlobalShortcut(mainWindow) {
globalShortcut.register('CommandOrControl+shift+r', () => {
app.relaunch()
app.exit()
})
globalShortcut.register('CommandOrControl+shift+delete', () => {
resetLocalData()
})
globalShortcut.register('F1', () => {
shell.openExternal('https://tiven.cn/p/4dc21784/')
})
globalShortcut.register('F2', () => {
shell.openExternal('https://tiven.cn/service/tools/post-tool')
})
globalShortcut.register('F5', () => {
mainWindow?.reload()
})
globalShortcut.register('CommandOrControl+r', () => {
mainWindow?.reload()
})
globalShortcut.register('CommandOrControl+q', () => {
app.exit()
})
globalShortcut.register('CommandOrControl+w', () => {
mainWindow?.hide()
mainWindow?.setSkipTaskbar(true)
})
globalShortcut.register('F11', () => {
// 是否全屏
if (mainWindow?.isFullScreen()) {
// mainWindow?.minimize();
mainWindow?.setFullScreen(false)
mainWindow?.setMenuBarVisibility(true)
} else {
mainWindow?.setFullScreen(true)
mainWindow?.setMenuBarVisibility(false)
}
})
globalShortcut.register('Esc', () => {
// 是否全屏
if (mainWindow?.isFullScreen()) {
// mainWindow?.minimize();
mainWindow?.setFullScreen(false)
mainWindow?.setMenuBarVisibility(true)
}
})
globalShortcut.register('CommandOrControl+F12', () => {
mainWindow?.webContents.openDevTools({ mode: 'detach' })
})
}

解决冲突

一般来说会在 mainWindow ready-to-show 的时候初始化快捷键,当然也可以在应用失去焦点(blur)的时候主动应用的注销快捷键,以避免和系统快捷键冲突。

1
2
3
4
5
6
7
8
9
10
11
12

mainWindow.on('ready-to-show', () => {
mainWindow.show()
// 注册全局快捷键
initGlobalShortcut(mainWindow)
})

mainWindow.on('blur', () => {
// 失去焦点,注销快捷键
globalShortcut.unregisterAll()
})


欢迎访问:天问博客

本文作者: Tiven
发布时间: 2023-07-01
最后更新: 2023-07-17
本文标题: electron globalShortcut 快捷键与系统全局快捷键冲突
本文链接: https://www.tiven.cn/p/6da36e35/
版权声明: 本作品采用 CC BY-NC-SA 4.0 许可协议进行许可。转载请注明出处!
欢迎留言,提问 ^_^
个人邮箱: tw.email@qq.com
notification icon
博客有更新,将会发送通知给您!