How to write text into external app from Electron app

49 views Asked by At

I'm building an Electron app for Mac, Windows, and Ubuntu.

This works fine on Mac.

The message gets written to whatever app had focus before my app did (and now has focus again, now that my Electron app is getting minimized).

It doesn't work for Windows or Ubuntu though.

How can I fix it?

import { keyboard } from '@nut-tree/nut-js';
import robot from '@jitsi/robotjs'; // https://github.com/jitsi/robotjs
// ...

async function insertMessage() {
    const messageToInsert = 'demo';
    if (isMac) {
      await keyboard.type(messageToInsert);
    } else {
      robot.typeString(messageToInsert);
    }
}

app.on('browser-window-blur', () => {
  console.log('app.on browser-window-blur');

  setTimeout(insertMessage, 150);
});
1

There are 1 answers

0
Ryan On

It seems from e.g. this comment that the only way (currently) to allow an Ubuntu Wayland app to write text to another app is for the Ubuntu user to manually set permissions in a way that would violate security best practices.