3

I am playing with a small electron app to make a simple copy/paste method.

I have registered a hotkey using globalShortcut:

globalShortcut.register(mod + '+' + key, () => { 
 clipboard.writeText(content);
 // Paste content to any input field/app
});

Is it possible for to now go to notepad and press the registered modifier to paste the content?

Example: App loads, registers a shortcut which sets the clipboard with their desired text.

They then go to a form where they want to paste this content and hit their key which pastes it for them?

This is essentially a way for staff to set up common shortcut / snippets of text with whatever key combinations they want. So if they are filling out report 123, they can just hit their key "Ctrl + Shift + R" which pastes the content they have associated with that hotkey.

How can I do this or simulate a Ctrl V to trigger it?

I have tried both RobotJS (doesn't support global shortcuts) and a Java version (preferred to not use anyway).

SBB
  • 8,560
  • 30
  • 108
  • 223
  • Was [this](https://www.npmjs.com/package/node-key-sender) the Java approach? Did that work and you want to find another solution? – snwflk Apr 03 '19 at 22:21
  • The java approach didn’t work but even so i found out that not every machine this would run on would have the runtime setup anyway so it could be a stopper. – SBB Apr 03 '19 at 22:22

3 Answers3

4

It worked for me using the following code:

const { app, globalShortcut } = require('electron')
const robot = require('robotjs')

app.on('ready', () => {
    globalShortcut.register('Control+Shift+R', () => {

        console.log('Control+Shift+R is pressed')

        // simulate CTRL+V / CMD+V
        setTimeout(() => {
            robot.keyTap('v', process.platform==='darwin' ? 'command' : 'control')
        }, 150)
    })
})

app.on('will-quit', () => {
    globalShortcut.unregisterAll()
})

The "trick" here is to delay the simulated key press by a certain interval in order to untangle the actual, physical key press and the simulated one. With shorter intervals, I often saw a "v" appear.

You'll need to decide whether to go for a longer delay (less user friendly because of long "wait", but unlikely to mix up keys) or a shorter delay (prompt response, more likely to get wrong result due to key press mixup).

If we're talking about highly repetitive work or big chunks of text, this will probably still be a timesaver.

snwflk
  • 3,341
  • 4
  • 25
  • 37
  • Wow, I think this did it! I tried robotjs for a while but didn't think about the delays. I don't even notice the timeout so it's not a problem at all. I tested this on my mac using command instead of control but it sounds like you were able to run it on windows as well so thats perfect. You are a lifesaver :) – SBB Apr 03 '19 at 23:17
  • Do you know if there is anything specific about this module that would cause it to not run in a build, but work fine otherwise? The keytap works great until I package it an run the app, then nothing happens, the console shows the key triggered though. – SBB Apr 04 '19 at 21:32
  • On a Mac it looks like the app needs to be allowed permission within the system preferences > accessibility to "Control" the Mac. I assume this is due to the clipboard access. Ill package it for windows now and see if anything like that happens. – SBB Apr 04 '19 at 21:46
2

This is due to the lack of the application’s menu with keybindings to the native clipboard. This can be set by creating your own keybidings, which are called accelerators.

In order to be copy/paste, you need to use content.paste() or content.copy(), this methods execute the editing command paste or copy in web page.

So, you could set an accelerator that would call an editing command, such as copy or paste.

Example:

const { app, globalShortcut } = require('electron')

app.on('ready', () => {
  // Register a 'CommandOrControl+Y' shortcut listener.
  globalShortcut.register('CommandOrControl+C', () => {
    // Do stuff when Y and either Command/Control is pressed.
    contents.copy()
  })

})

contents doc

accelerators docs

  • 1
    This works fine as I would expect when I am trying to paste the content within the `mainWindow` that was created. The goal of this is to be able to paste content OUTSIDE of the application itself. ie, App loads, I minimize it and go to notepad, press the hotkey, and then the content gets pasted. – SBB Apr 03 '19 at 23:00
0

This issue solved here below, but use Menu from electron

const { Menu } = require('electron')

instead of using

var Menu = require("menu");

In the code from https://stackoverflow.com/a/43584935/12382178