Supporting copy and paste in electron

Khoa Pham
Indie Goodies
Published in
2 min readFeb 18, 2019

--

I use electron.js for my many desktop apps. One of them is PushNotification which is very helpful for testing push notifications in iOS and Android apps.

It works well until I package it, I can’t copy and paste using hot keys anymore. It turns out that this is due to electron-packager that I use. After running electron-packager, the app does not accept copy, paste anymore. This is because the release build does not have menu with key binding to the clipboard by default. We can solve this by manually declaring the menu.

Menu in electron.js

Menu component in electron is for native application menus and context menus. It is is only available in the main process, but you can also use it in the render process via the remote module. Depending on the platform, macOS has a completely different style of application menu from Windows and Linux.

Menu exposes accelerator, which are Strings that can contain multiple modifiers and a single key code, combined by the +character, and are used to define keyboard shortcuts throughout your application.

Accelerator is very useful for supporting Keyboard shortcuts. You can use the Menu module to configure keyboard shortcuts that will be triggered only when the app is focused. To do so, specify an accelerator property when creating a…

--

--