pdcc
pdcc

Reputation: 365

simulate Ctrl+V in electron app

I am working on an electron app and I have a globalShortcut.register event. What i want is, when user press this shortcut, hide my app window and simulate a paste (Ctrl+V) in the background app like a text editor. This can be made with electron?

Upvotes: 1

Views: 1830

Answers (1)

unseen_damage
unseen_damage

Reputation: 1376

If you are looking to use the clipboard, to copy something from your Electron app, use the following code. This makes use of the clipBoard method:

const clipboard = require('electron').clipboard

const copyBtn = document.getElementById('copy-to')
const copyInput = document.getElementById('copy-to-input')

copyBtn.addEventListener('click', function () {
  if (copyInput.value !== '') copyInput.value = ''
  copyInput.placeholder = 'Copied! Paste here to see.'
  clipboard.writeText('Electron Demo!')
})

If you are looking to do something when a certain key combo is hit (like Ctrl+V) you can use an accelerator or create your own combinations with globalShortcut.

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

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

Upvotes: 1

Related Questions