• Electron在mac下快捷键失效的问题及解决


    场景:在消息发送的输入框中,使用快捷键的复制粘贴,全选,等等都会失效。

    解决方案如下:

    将如下代码放到main/index.js主进程中

    mainWIndow = new BrowserWindow({}); 
    if (process.platform === 'darwin') { let contents = mainWindow.webContents globalShortcut.register('CommandOrControl+C', () => { contents.copy() }) globalShortcut.register('CommandOrControl+V', () => { contents.paste() }) globalShortcut.register('CommandOrControl+X', () => { contents.cut() }) globalShortcut.register('CommandOrControl+A', () => { contents.selectAll() }) }

    补充:在实践了上述方案,发现一个后续的问题,electron项目的快捷键可以使用了,但是一个更严重的问题是,系统本身的快捷键由于被electron全局注册,因此系统的快捷键失效。

    面临这个问题的解决办法,我们进行以下方法修复

      mainWindow.on('focus', () => {
        // mac下快捷键失效的问题
        if (process.platform === 'darwin') {
          let contents = mainWindow.webContents
          globalShortcut.register('CommandOrControl+C', () => {
            console.log('注册复制快捷键成功')
            contents.copy()
          })
    
          globalShortcut.register('CommandOrControl+V', () => {
            console.log('注册粘贴快捷键成功')
            contents.paste()
          })
    
          globalShortcut.register('CommandOrControl+X', () => {
            console.log('注册剪切快捷键成功')
            contents.cut()
          })
    
          globalShortcut.register('CommandOrControl+A', () => {
            console.log('注册全选快捷键成功')
            contents.selectAll()
          })
        }
      })
      mainWindow.on('blur', () => {
        globalShortcut.unregisterAll() // 注销键盘事件
      })

    思路:在窗口获取焦点的时候注册快捷键,在窗口失去焦点的时候注销electron的快捷键即可。

  • 相关阅读:
    Smarty学习笔记(二)
    Smarty学习笔记(一)
    MVC学习笔记(一)
    2015羊年主流手机配置什么样?
    FPGA学习笔记(一)Verilog语法基础
    FPGA学习笔记(二)模块建立及变量连接
    STM32学习笔记(一)时钟和定时器
    Win8 HTML5与JS编程学习笔记(一)
    Win8 HTML5与JS编程学习笔记(二)
    LUOGU P2831 愤怒的小鸟 (NOIP 2016)
  • 原文地址:https://www.cnblogs.com/bgwhite/p/12706285.html
Copyright © 2020-2023  润新知