Is it possible to simulate keyboard/mouse event in NodeJS?
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.
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])
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)
You could use possibly use RobotJS for this.
Example code:
var robot = require("robotjs");
// Type user's password or something.
robot.typeString("abc123");