I did it for myself because I didn't feel like dealing with libraries like 'node-ffi' to implement this functionality. Maybe someone will find it useful. It's WINDOWS OS ONLY
This package is a native addon for Node.js that allows you to perform various utility operations on Windows systems. It includes key event listeners, window data retrieval, window screenshot capture functionality, mouse movement, mouse click, mouse drag, and typing functionality, also I included precompiled libs of OpenCV(core, imgcodecs, imgproc)
Added a new function to simulate button press and release (keyPress
), introduced an enumeration KeyCodesHelper
with the most common key codes, and fixed a bug where the typeString
function was not working with languages other than English.
You can install the package using npm:
npm install node-native-win-utils
To use the package, import the necessary functions, types, and classes:
import {
keyDownHandler,
keyUpHandler,
getWindowData,
captureWindow,
captureWindowN,
mouseMove,
mouseClick,
mouseDrag,
typeString,
OpenCV,
} from "node-native-win-utils";
The package provides keyDownHandler
and keyUpHandler
functions, which allow you to register callbacks for key down and key up events, respectively. The callbacks receive the keyCode
as a parameter:
keyDownHandler((keyCode) => {
console.log("Key down:", keyCode);
});
// Key down: 8
keyUpHandler((keyCode) => {
console.log("Key up:", keyCode);
});
// Key up: 8
The keyPress function allows you to simulate a key press event. Provide the keyCode as a parameter and optionally the number of times to press the key:
keyPress(65); // Press 'A' key once
keyPress(65, 3); // Press 'A' key three times
The KeyCodeHelper enum provides a set of key codes that can be used with key event functions:
import { KeyCodeHelper } from "node-native-win-utils";
console.log(KeyCodeHelper.A); // Outputs the key code for 'A'
The getWindowData
function retrieves information about a specific window identified by its name. It returns an object with properties width
, height
, x
, and y
, representing the window dimensions and position:
const windowData = getWindowData("Window Name");
console.log("Window data:", windowData);
// Window data: { width: 800, height: 600, x: 50, y: 50 }
The captureWindow
function allows you to capture a screenshot of a specific window identified by its name. Provide the window name and the output path as parameters:
captureWindow("Window Name", "output.png");
// Output: output.png with a screenshot of the window
The mouseMove
function allows you to move the mouse to a specific position on the screen. Provide the posX
and posY
coordinates as parameters:
mouseMove(100, 200);
The mouseClick
function allows you to perform a mouse click event. Optionally, you can specify the mouse button as a parameter ("left"
, "middle"
, or "right"
). If no button is specified, a left mouse click is performed by default:
mouseClick(); // Left mouse click
mouseClick("right"); // Right mouse click
The mouseDrag
function allows you to simulate dragging the mouse from one position to another. Provide the starting and ending coordinates (startX
, startY
, endX
, endY
) as parameters. Optionally, you can specify the speed at which the mouse should be dragged:
mouseDrag(100, 200, 300, 400);
mouseDrag(100, 200, 300, 400, 100); // Drag with speed 100
The typeString
function allows you to simulate typing a string of characters. Provide the string to type as the stringToType
parameter. Optionally, you can specify
a delay between each character (in milliseconds) using the delay
parameter:
typeString("Hello, world!");
typeString("Hello, world!", 100); // Type with a delay of 100ms between characters
The KeyListener
class extends the EventEmitter class and simplifies working with the keyDownHandler
and keyUpHandler
functions. You can register event listeners for the "keyDown" and "keyUp" events using the on
method:
const listener = new KeyListener();
listener.on("keyDown", (data) => {
console.log("Key down:", data.keyCode, data.keyName);
});
// Key down: 8 Backspace
listener.on("keyUp", (data) => {
console.log("Key up:", data.keyCode, data.keyName);
});
// Key up: 8 Backspace
The OpenCV
class extends the capabilities of the native addon package by providing various image processing functionalities. It allows users to perform operations such as matching templates, blurring images, converting color formats, drawing rectangles, getting image regions, and writing images to files.
const image = new OpenCV(image: string | ImageData)
Creates a new instance of the OpenCV
class with the specified image data. The image
parameter can be either a file path (string) or an existing ImageData
object.
Holds the underlying image data that will be used for image processing operations.
Read-only property that returns the width of the image in pixels.
Read-only property that returns the height of the image in pixels.
Matches a template image with the current image and returns a new OpenCV
instance containing the result.
-
template: ImageData
: The template image data to be matched. -
method?: number | null
: (Optional) The matching method to be used. If not provided, the default method will be used.(currently no implemented) -
mask?: ImageData
: (Optional) An optional mask image data to be used during the matching process.
Applies a blur filter to the current image and returns a new OpenCV
instance containing the blurred result.
-
sizeX: number
: The size of the blur kernel in the X direction. -
sizeY: number
: The size of the blur kernel in the Y direction.
Converts the current image from the BGR color format to grayscale and returns a new OpenCV
instance containing the grayscale result.
Draws a rectangle on the current image and returns a new OpenCV
instance containing the modified result.
-
start: Point
: The starting point (top-left) of the rectangle. -
end: Point
: The ending point (bottom-right) of the rectangle. -
rgb: Color
: The color of the rectangle in the RGB format (e.g.,{ r: 255, g: 0, b: 0 }
for red). -
thickness: number
: The thickness of the rectangle's border.
Extracts a region of interest (ROI) from the current image and returns a new OpenCV
instance containing the extracted region.
-
region: ROI
: An object specifying the region of interest with propertiesx
,y
,width
,height
.
Writes the current image to a file specified by the path
.
-
path: string
: The file path where the image will be saved.
Function | Parameters | Return Type |
---|---|---|
keyDownHandler | callback: (keyCode: number) => void |
void |
keyUpHandler | callback: (keyCode: number) => void |
void |
getWindowData | windowName: string |
WindowData |
captureWindow | windowName: string, outputPath: string |
void |
mouseMove | posX: number, posY: number |
boolean |
mouseClick | button?: "left" | "middle" | "right" |
boolean |
mouseDrag | startX: number, startY: number, endX: number, endY: number, speed?: number |
boolean |
typeString | stringToType: string, delay?: number |
boolean |
captureWindowN | windowName: string |
Buffer |
keyPress | keyCode: number, repeat?: number |
boolean |
Here are some examples of using the package:
// Example usage of the OpenCV class
import { OpenCV } from "node-native-win-utils";
const image = new OpenCV("path/to/image.png");
const template = new OpenCV("path/to/template.png");
const matchedImage = image.matchTemplate(template.imageData);
const blurredImage = image.blur(5, 5);
const grayscaleImage = image.bgrToGray();
const regionOfInterest = { x: 100, y: 100, width: 200, height: 150 };
const regionImage = image.getRegion(regionOfInterest);
const redColor = { r: 255, g: 0, b: 0 };
const thickRectangle = image.drawRectangle(
{ x: 50, y: 50 },
{ x: 150, y: 150 },
redColor,
3
);
matchedImage.imwrite("output/matched.png");
blurredImage.imwrite("output/blurred.png");
grayscaleImage.imwrite("output/grayscale.png");
regionImage.imwrite("output/region.png");
thickRectangle.imwrite("output/thick_rectangle.png");
// If you want to aply blur and convert to gray then do it that order:
image.blur(5, 5).bgrToGray();
// Otherwise you will get an error.
Please note that the above example demonstrates the usage of different methods available in the OpenCV
class. Make sure to replace "path/to/image.png"
and "path/to/template.png"
with actual image file paths.
import {
keyDownHandler,
keyUpHandler,
getWindowData,
captureWindow,
mouseMove,
mouseClick,
mouseDrag,
typeString,
KeyListener,
} from "node-native-win-utils";
// Register key event handlers
keyDownHandler((keyCode) => {
console.log("Key down:", keyCode);
});
// Key down: 123
keyUpHandler((keyCode) => {
console.log("Key up:", keyCode);
});
// Key up: 123
// Retrieve window data
const windowData = getWindowData("My Window");
console.log("Window data:", windowData);
// Window data: { width: 1024, height: 768, x: 100, y: 100 }
// Capture window screenshot
captureWindow("My Window", "output.png");
// Output: output.png with a screenshot of the window
// Move the mouse
mouseMove(100, 200);
// Perform mouse click
mouseClick(); // Left mouse click
mouseClick("right"); // Right mouse click
// Simulate mouse drag
mouseDrag(100, 200, 300, 400);
mouseDrag(100, 200, 300, 400, 100); // Drag with speed 100
// Simulate typing
typeString("Hello, world!");
typeString("Hello, world!", 100); // Type with a delay of 100ms between characters
// Use KeyListener class
const listener = new KeyListener();
listener.on("keyDown", (data) => {
console.log("Key down:", data.keyCode, data.keyName);
});
// Key down: keyCode keyName
listener.on("keyUp", (data) => {
console.log("Key up:", data.keyCode, data.keyName);
});
// Key up: keyCode keyName
-
Write proper tests
-
Add more useful functions
-
Fix issue with Windows scale for font,text,apps