如何在ElectronJS中禁用键盘快捷键'Alt + F4'



我无法在Electron中禁用或更改这些键盘快捷键的工作。

Ctrl+Alt+DeleteAlt+F4

下面我提到了进行此更改的 2 个不同代码。


const { app, BrowserWindow, globalShortcut, path, url} = require('electron')
app.on('ready', () => {
const ret = globalShortcut.register('alt+f4', function () {
win.show()
})
})

{
role: 'help',
submenu: [
{
label: 'Reload', 
accelerator: 'Alt + F4',
click: function (item, focusedWindow) {
focusedWindow.reload();
} 
}
]
}

您可以通过阻止进程renderer上的keydown事件来禁用它

window.addEventListener('keydown', (e) => {
const { key, altKey } = e;
if (key === 'F4' && altKey) {
e.preventDefault();   
}
});

尝试将以下代码用于 alt+f4:

window.webContents
.on("before-input-event",
(event,input)=>
{ 
if(input.code=='F4'&&input.alt) 
event.preventDefault();
}
);

最新更新