【问题标题】:How to disable keyboard shortcuts 'Alt + F4' in ElectronJS如何在 ElectronJS 中禁用键盘快捷键“Alt + F4”
【发布时间】:2019-03-22 09:45:12
【问题描述】:

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

ctrl+alt+删除alt+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();
            } 
        }
    ]
}

【问题讨论】:

  • 谢谢,但我们使用了 ElectronJS。
  • 首先将您的 BrowserWindow 选项设置为 closable: false 这应该会阻止 'Alt+F4'
  • 嗨 Leonardo,感谢您的帮助,但这不适用于我的 Windows 应用程序。下面我提到了代码:** function createWindow () { // 创建浏览器窗口。 win = new BrowserWindow({ width: 1080, height: 720, frame: false, resizable: false, alwaysOnTop:true, closable:false })**

标签: javascript node.js electron


【解决方案1】:

尝试使用下面的代码 alt+f4:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      • 2023-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多