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 e 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.
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 e 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.
Share Improve this question asked Nov 21, 2015 at 19:34 SchlausSchlaus 19.3k11 gold badges39 silver badges68 bronze badges 3- 2 Have you seen this? github./octalmage/robotjs – mr.freeze Commented Nov 21, 2015 at 20:00
- 1 @nfreeze I had not, but it looks to be just what I was looking for! – Schlaus Commented Nov 21, 2015 at 20:08
- 1 @nfreeze Yep, RobotJS solves the problem. Please post an answer so I can accept it! – Schlaus Commented Nov 21, 2015 at 22:18
3 Answers
Reset to default 4I would give RobotJS a try. It appears to do what you need.
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./package/node-key-sender.
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 pile Rust into Node.js native addons.
So yeah, with these updates, I was able to pile 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./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("
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745436263a4627621.html
评论列表(0条)