Home > Software design >  Is there any Windows library in node.js (like Windows.h in c )
Is there any Windows library in node.js (like Windows.h in c )

Time:12-25

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?

CodePudding user response:

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.

CodePudding user response:

to can user robotjs lib https://www.npmjs.com/package/robotjs

//Type "Hello World" then press enter.
var robot = require("robotjs");
 
//Type "Hello World".
robot.typeString("Hello World");
 
// Press enter.
robot.keyTap("enter");
  • Related