Search code examples
macoscross-platformelectronelectron-packager

Shortcuts in Electron on Mac


Well, I have quite a simple task, which can't be really hard. I have an app, which uses the electron framework to use the application on Windows and Mac machines. I notices that I am able to use Ctrl+c/Ctrl+v on Windows without any problems, but I am not able to use Cmd+c/Cmd+v on Mac after I used the electron-packager.

I found solutions like this (CMD + C / CMD + V not working), but I have a custom menue and I don't want to define and use the one in electron itself. So I found this (global accelerators without menue, but the issue is still open and there seems to be no solution. Solutions like this (Local-Shortcut) are also not usable, as they don't get the selected text (e.g. from a textbox) as a parameter.

I think using Cmd+c / Cmd+v shouldn't be a real issue, since it is a common action in every application, but I don't see a usable solution at the moment. This also effects all other shortcuts like Cmd+a to select everything.


Solution

  • There seems to be no way of doing it if you really want to hide these shortcuts from the menu.

    At the moment, the best workaround is to display the shortcuts menu on MacOS only:

    const { Menu } = require('electron')
    
    const menuTemplate = [...];
    
    if (process.platform === 'darwin') {
      menuTemplate.push({
        label: 'Edit',
        submenu: [
          {role: 'undo'},
          {role: 'redo'},
          {type: 'separator'},
          {role: 'cut'},
          {role: 'copy'},
          {role: 'paste'},
          {role: 'pasteandmatchstyle'},
          {role: 'delete'},
          {role: 'selectall'}
        ]
      })
    }
    
    const applicationMenu = Menu.buildFromTemplate(menuTemplate)
    Menu.setApplicationMenu(applicationMenu)
    

    https://blog.avocode.com/blog/4-must-know-tips-for-building-cross-platform-electron-apps