Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is there any Windows library in node.js (like Windows.h in c++)

I wanted to make a script which can move my cursor to a certain point on the screen and click or type something. Is there any way to do that with node.js?

I am aware that I can do that with c++ using <Windows.h> or <WinUser.h> headers. But I prefer using JavaScript so I was wondering is there any way to do that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

you can use with Node.js is the robotjs library. This library provides a simple, cross-platform way to control the mouse and keyboard using JavaScript.

npm install robotjs

Here is an example from its website:

// Move the mouse across the screen as a sine wave.
var robot = require("robotjs");
 
// Speed up the mouse.
robot.setMouseDelay(2);
 
var twoPI = Math.PI * 2.0;
var screenSize = robot.getScreenSize();
var height = (screenSize.height / 2) - 10;
var width = screenSize.width;
 
for (var x = 0; x < width; x++)
{
    y = height * Math.sin((twoPI * x) / width) + height;
    robot.moveMouse(x, y);
}
Keyboard
// Type "Hello World" then press enter.
var robot = require("robotjs");
 
// Type "Hello World".
robot.typeString("Hello World");
 
// Press enter.
robot.keyTap("enter");
Screen
// Get pixel color under the mouse.
var robot = require("robotjs");
 
// Get mouse position.
var mouse = robot.getMousePos();
 
// Get pixel color in hex format.
var hex = robot.getPixelColor(mouse.x, mouse.y);
console.log("#" + hex + " at x:" + mouse.x + " y:" + mouse.y);

For more details you can go with its documentation.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading