Shortcuts in Electron on Mac
Asked Answered
W

1

7

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.

Wasserman answered 28/6, 2017 at 8:26 Comment(6)
What's the issue with local/global shortcuts?Imogen
There is no way to get the selected text as a parameter. At least I couldn't find a way to get it.Wasserman
What about https://github.com/electron/electron/blob/master/docs/api/clipboard.md?Imogen
Yeah sure... this is not the problem, I am using this plugin already for other usecases... The thing is, I need to capture the cmd-xy event and in case of copy I need to know the selected text / in case of insert, the focused element. But I don't think that this is the right way of doing it.Wasserman
I don't get the first part of your question. I'm pretty sure it's not giving you copied text/focused element on Windows either. Does it?Imogen
No it doesn't but on a Windows machine copy, paste, select all and all other shortcuts are working fine without any further efforts. This just affects macOS. I am trying to find a workaround here.Wasserman
I
3

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

Imogen answered 28/6, 2017 at 11:10 Comment(1)
This behaviour is made by design as stated in electron/electron#3787. However, the menue approach is working in general. The individual code needs to be called after the ready state for the app objectWasserman

© 2022 - 2024 — McMap. All rights reserved.