我正在试图禁用所有键盘快捷键在我的electron.js应用程序。
我尝试了以下方法(扰流板警告:它们没有工作):
globalShortcut.unregisterAll()
和
globalShortcut.register('Alt+CommandOrControl+A', () => {
console.log('not allowed')
})
globalShortcut.register('Alt+CommandOrControl+B', () => {
console.log('not allowed')
})
globalShortcut.register('Alt+CommandOrControl+C', () => {
console.log('not allowed')
})
globalShortcut.register('Alt+CommandOrControl+D', () => {
console.log('not allowed') // and so on
})(我对所有密钥(从A到Z、1到9等)执行了^操作。顺便说一句,我尝试的所有代码都放在app.whenReady()函数中。
这些都不管用。我用其他更抽象的方法看了很多文章,但它们也没有用。实际上,我也试图寻找一个npm包,但我没有找到任何可以解决我的问题。
我只需要完全禁用我的电子应用程序的所有键盘快捷键。还有其他方法(实际可行)吗?
发布于 2021-09-30 19:35:35
以下是简单的解决方案:
app.on('browser-window-focus', function () {
globalShortcut.register("CommandOrControl+R", () => {
console.log("CommandOrControl+R is pressed: Shortcut Disabled");
});
globalShortcut.register("F5", () => {
console.log("F5 is pressed: Shortcut Disabled");
});
});app.on('browser-window-blur', function () {
globalShortcut.unregister('CommandOrControl+R');
globalShortcut.unregister('F5');
});您可以禁用所有的快捷方式,注册时,窗口是聚焦,并取消注册时,模糊。
请看这里的问题,Disable reload via keyboard shortcut electron app
编辑:禁用所有快捷键globalShortcut.registerAll(accelerators, callback)加速器String[] -一个加速器阵列。回调函数注册加速器中所有加速器项的全局快捷方式。当用户按下任何已注册的快捷方式时,就会调用回调。
globalShortcut.unregisterAll()取消注册所有全局快捷方式。
https://stackoverflow.com/questions/69348406
复制相似问题