Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to simulate keyboard/mouse event in NodeJS?

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?

like image 927
jayarjo Avatar asked Jun 24 '12 14:06

jayarjo


People also ask

Can JavaScript simulate key presses?

If you need keyup events too, it is also possible to simulate keyup events by changing "keydown" to "keyup" in the code snippet. This also sends the event to the entire webpage, hence the document . If you want only a specific element to receive the event, you can substitute document for the desired element.

What is Iohook?

Iohook is a Node. js global native keyboard and mouse listener. That (Iohook) can handle keyboard and mouse events via native hooks inside and outside your javascript/typescript application. Iohook determines the event of left-click, right-click, scrolling and key down, key up, key pressed, etc.

How do you press a key in JavaScript?

To record a keypress event in JavaScript, use the code below: // Add event listener on keypress document. addEventListener('keypress', (event) => { var name = event. key; var code = event.


1 Answers

You could use possibly use RobotJS for this.

Example code:

var robot = require("robotjs");

// Type user's password or something. 
robot.typeString("abc123");
like image 190
Jason Stallings Avatar answered Sep 26 '22 18:09

Jason Stallings