Reputation: 19212
From an Electron application, is it possible to output text to wherever the cursor is currently located at, i.e. even if that is somewhere outside of the actual Electron app?
So far the best solution I've come up with is to write the text on to the clipboard, and notify the user that the text is ready to be pasted. I'd like to get rid of that extra step.
Upvotes: 4
Views: 1886
Reputation: 155
I tried using RobotJS
, but couldn't get it to work with electron as of today 2023. node-key-sender
requires a Java Run Time, which isn't as nice as a system native util that doesn't require a run time as dependency.
This question was originally asked in 2015. Fast forward today, there have been great advancements in Node.js and tools. Node.js has released Node-API(NAPI) since version 8, which has made it easy to interface with native addons. There are also tools like NAPI-RS that allows you to easily compile Rust into Node.js native addons.
So yeah, with these updates, I was able to compile the Rust enigo text
util into a Node.js package using NAPI-RS that allows you to insert text easily. You can read more about the source code and implementation at https://github.com/xitanggg/node-insert-text.
(One very nice thing about the NAPI-RS tooling is that the binary has been built, so this package just works after installation, i.e. no need to build it yourself. Also, the binary is selectively installed, meaning installation only installs the binary that your system needs, e.g. windows or Mac, to keep the size small instead of including all binaries at once.)
To try it out, you can install it with
npm i @xitanggg/node-insert-text
and run the following code snippet:
import {insertText} from '@xitanggg/node-insert-text';
insertText("👋Hello World! This line is inserted programmatically🤖")
Hope this helps anyone who stumbles upon this today. (So yeah, if you can't find any existing npm package that fits your needs, you can find a Rust alternative and compile it as Node.js native addons, and this is just a quick example)
Upvotes: 1
Reputation: 603
You may try an alternative to RobotJS. It is a very small and still cross platform library to send keys to your operational system called node-key-sender.
Install it with npm install --save-dev node-key-sender
.
And send a text to the keyboard using:
var ks = require('node-key-sender');
ks.sendText('This is my text');
Check out the documentation page: https://www.npmjs.com/package/node-key-sender.
Upvotes: 2