Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting/pasting text into active window?

I'd like to insert/paste a text in the currently active window and at the focussed element within there, which most likely won't be the app itself (but could be).

I already checked (close to) all clipboard packages / capabilities and it doesn't appear it is possible to initiate a paste event from there. Other solutions for inserting text only seem to be for within the app / mostly for inserting into files.

Tried creating a keyboard even in the rendered process with JS, but this would be limited to the renderer, and I think it should be in the main to even access anything outside the renderer.

Anyone know how to do this / confirm whether or not its even possible?

like image 670
Sormano Avatar asked Jun 17 '26 21:06

Sormano


2 Answers

You could do this using a combination of electron's clipboard module to get the contents of the clipboard and robotjs typeString method to type the string into other applications:

const { clipboard } = require('electron');
const robot = require('robotjs');
const text = clipboard.readText();
robot.typeString(text);
like image 102
Tim Avatar answered Jun 19 '26 10:06

Tim


If the text is already in your clipboard, why not just use the paste keyboard command (Ctrl+V)?

robot.keyTap('v', ['control']);
like image 25
Henry Avatar answered Jun 19 '26 12:06

Henry