Imagine that a NodeJS module, when invoked from console, outputs some introductory messages and then waits for user input (click enter or esc). This module already has and does everything we require, except that - wait-for-user-input prompt. So we wonder (I'm personally very new to NodeJS) if it is possible to execute console module programmatically and trigger an input event on it, so that it doesn't wait and proceed with the job right away?
5 Answers
You could use possibly use RobotJS for this.
Example code:
var robot = require("robotjs");
// Type user's password or something.
robot.typeString("abc123");

- 1,443
- 20
- 17
-
3This should be the accepted answer, I've just used it to simulate an enter key press successfully with `robot.keyTap('enter')`. – jviotti Aug 07 '15 at 19:55
-
Thanks! It's super easy, and cross platform! – Jason Stallings Aug 09 '15 at 07:23
-
3Sadly does not seem to be maintained at the moment... – frankenapps Nov 30 '20 at 14:34
As Jason mentioned you could use RobotJS for key simulation but there are couple of steps require to correctly build robotJS for Windows paltform:
- You would need windows build tools so run
npm install --global windows-build-tools
(would take some time as it's around 120MB) - run
npm install robotjs --save-dev
You're done!.
If this is for electron app then you would also require below 3rd step: run
npm rebuild --runtime=electron --target=1.7.9 --disturl=https://atom.io/download/atom-shell --abi=57
(1.7.9 is my
electron --version
and abi is for my correspondingnode --version
8.7 installed, you can check abi version for node version here [look for NODE_MODULE_VERSION column])

- 42,508
- 29
- 229
- 225
-
-
2@DLight That depends on the purpose of your package and the way this llibrary is used within it; if this package is a stand-alone application, then it doesn't matter. If this package is a library, then it should be in normal dependencies unless it is only used for development (since `devDependencies`) are not installed if the package was installed from NPM. – William Stanley Feb 03 '20 at 06:34
node-key-sender library is an alternative to RobotJs if you just need to send keys to your operational system. It is cross platform and very small lib.
Install it with npm install --save-dev node-key-sender
.
And send "enter" to the keyboard using:
var ks = require('node-key-sender');
ks.sendKey('enter');
Check out the documentation page: https://www.npmjs.com/package/node-key-sender.

- 583
- 5
- 6
-
16Unfortunately it requires a full blown java environment to work (it's just a wrapper for a jar file)! – devsnd Aug 30 '18 at 09:40
-
4
-
Because they saving it as developer depedency instead of saving it in to running packages – Sudhan Apr 28 '21 at 13:50
Responding to @Venryx. They are right that robotjs is going to have a delay, especially if you have to load node first, however, if you already have node loaded, it may be worth trying out
robot.setKeyboardDelay(0)
The default setting for a delay is 10ms. This helped me tremendously.

- 113
- 6
I've tried robotjs
and node-key-sender
, but they cause a substantial amount of delay/stuttering per key-event. (especially noticeable when sending them frequently)
To resolve this, I found a way to use node-ffi-napi
to call the Windows user32 SendInput
function directly: https://stackoverflow.com/a/50412529/2441655
In my case at least, this achieved substantially better performance. (however, a drawback is that it only works on Windows, of course)

- 15,624
- 10
- 70
- 96